// Copyright (C) Kevin Suffern 2000-2007.
// This C++ code is for non-commercial purposes only.
// This C++ code is licensed under the GNU General Public License Version 2.
// See the file COPYING.txt for the full license.
// This file contains the definition of the Grid class
#include <iostream>
#include <vector>
#include <math.h>
#include "Constants.h"
#include "Vector3D.h"
#include "Point3D.h"
#include "Grid.h"
//#include "MeshTriangle.h"
//#include "FlatMeshTriangle.h"
//#include "SmoothMeshTriangle.h"
//#include "FlatUVMeshTriangle.h"
//#include "SmoothUVMeshTriangle.h"
#include "Triangle.h"
#include "SmoothTriangle.h"
//#include "ply.h"
typedef enum {
flat,
smooth
} TriangleType;
// ---------------------------------------------------------------- default constructor
Grid::Grid(void)
: Compound(),
nx(0),
ny(0),
nz(0)
// mesh_ptr(new Mesh),
// reverse_normal(false)
{
// The cells array will be empty
}
// ---------------------------------------------------------------- constructor
// for rendering triangle meshes
Grid::Grid(Mesh* _mesh_ptr)
: Compound(),
nx(0),
ny(0),
nz(0)
// mesh_ptr(_mesh_ptr),
// reverse_normal(false)
{
// The cells array will be empty
}
// ---------------------------------------------------------------- clone
Grid*
Grid::clone(void) const {
return (new Grid (*this));
}
// ---------------------------------------------------------------- copy constructor
// not implemented
Grid::Grid(const Grid& grid) {}
// ---------------------------------------------------------------- assignment operator
// not implemented
Grid&
Grid::operator= (const Grid& rhs) {
return (*this);
}
// ---------------------------------------------------------------- destructor
// not implemented
Grid::~Grid(void) {}
BBox
Grid::get_bounding_box(void) {
return (bbox);
}
//------------------------------------------------------------------ setup_cells
void
Grid::setup_cells(void) {
// find the minimum and maximum coordinates of the grid
Point3D p0 = find_min_bounds();
Point3D p1 = find_max_bounds();
bbox.x0 = p0.x;
bbox.y0 = p0.y;
bbox.z0 = p0.z;
bbox.x1 = p1.x;
bbox.y1 = p1.y;
bbox.z1 = p1.z;
// compute the number of grid cells in the x, y, and z directions
int num_objects = objects.size();
// dimensions of the grid in the x, y, and z directions
double wx = p1.x - p0.x;
double wy = p1.y - p0.y;
double wz = p1.z - p0.z;
double multiplier = 2.0; // multiplyer scales the number of grid cells relative to the number of objects
double s = pow(wx * wy * wz / num_objects, 0.3333333);
nx = multiplier * wx / s + 1;
ny = multiplier * wy / s + 1;
nz = multiplier * wz / s + 1;
// set up the array of grid cells with null pointers
int num_cells = nx * ny * nz;
cells.reserve(num_objects);
for (int j = 0; j < num_cells; j++)
cells.push_back(NULL);
// set up a temporary array to hold the number of objects stored in each cell
vector<int> counts;
counts.reserve(num_cells);
for (int j = 0; j < num_cells; j++)
counts.push_back(0);
// put the objects into the cells
BBox obj_bBox; // object's bounding box
int index; // cell's array index
for (int j = 0; j < num_objects; j++) {
obj_bBox = objects[j]->get_bounding_box();
// compute the cell indices at the corners of the bounding box of the object
int ixmin = clamp((obj_bBox.x0 - p0.x) * nx / (p1.x - p0.x), 0, nx - 1);
int iymin = clamp((obj_bBox.y0 - p0.y) * ny / (p1.y - p0.y), 0, ny - 1);
int izmin = clamp((obj_bBox.z0 - p0.z) * nz / (p1.z - p0.z), 0, nz - 1);
int ixmax = clamp((obj_bBox.x1 - p0.x) * nx / (p1.x - p0.x), 0, nx - 1);
int iymax = clamp((obj_bBox.y1 - p0.y) * ny / (p1.y - p0.y), 0, ny - 1);
int izmax = clamp((obj_bBox.z1 - p0.z) * nz / (p1.z - p0.z), 0, nz - 1);
// add the object to the cells
for (int iz = izmin; iz <= izmax; iz++) // cells in z direction
for (int iy = iymin; iy <= iymax; iy++) // cells in y direction
for (int ix = ixmin; ix <= ixmax; ix++) { // cells in x direction
index = ix + nx * iy + nx * ny * iz;
if (counts[index] == 0) {
cells[index] = objects[j];
counts[index] += 1; // now = 1
}
else {
if (counts[index] == 1) {
Compound* compound_ptr = new Compound; // construct a compound object
compound_ptr->add_object(cells[index]); // add object already in cell
compound_ptr->add_object(objects[j]); // add the new object
cells[index] = compound_ptr; // store compound in current cell
counts[index] += 1; // now = 2
}
else { // counts[index] > 1
cells[index]->add_object(objects[j]); // just add current object
counts[index] += 1; // for statistics only
}
}
}
} // end of for (int j = 0; j < num_objects; j++)
// erase the Compound::vector that stores the object pointers, but don't delete the objects
objects.erase (objects.begin(), objects.end());
// display some statistics on counts
// this is useful for finding out how many cells have no objects, one object, etc
// comment this out if you don't want to use it
int num_zeroes = 0;
int num_ones = 0;
int num_twos = 0;
int num_threes = 0;
int num_greater = 0;
for (int j = 0; j < num_cells; j++) {
if (counts[j] == 0)
num_zeroes += 1;
if (counts[j] == 1)
num_ones += 1;
if (counts[j] == 2)
num_twos += 1;
if (counts[j] == 3)
num_threes += 1;
if (counts[j] > 3)
num_greater += 1;
}
cout << "num_cells =" << num_cells << endl;
cout << "numZeroes = " << num_zeroes << " numOnes = " << num_ones << " numTwos = " << num_twos << endl;
cout << "numThrees = " << num_threes << " numGreater = " << num_greater << endl;
// erase the temporary counts vector
counts.erase (counts.begin(), counts.end());
}
//------------------------------------------------------------------ find_min_bounds
// find the minimum grid coordinates, based on the bounding boxes of all the objects
Point3D
Grid::find_min_bounds(void) {
BBox object_box;
Point3D p0(kHugeValue);
int num_objects = objects.size();
for (int j = 0; j < num_objects; j++) {
object_box = objects[j]->get_bounding_box();
if (object_box.x0 < p0.x)
p0.x = object_box.x0;
if (object_box.y0 < p0.y)
p0.y = object_box.y0;
if (object_box.z0 < p0.z)
p0.z = object_box.z0;
}
p0.x -= kEpsilon; p0.y -= kEpsilon; p0.z -= kEpsilon;
return (p0);
}
//------------------------------------------------------------------ find_max_bounds
// find the maximum grid coordinates, based on the bounding boxes of the objects
Point3D
Grid::find_max_bounds(void) {
BBox object_box;
Point3D p1(-kHugeValue);
int num_objects = objects.size();
for (int j = 0; j < num_objects; j++) {
object_box = objects[j]->get_bounding_box();
if (object_box.x1 > p1.x)
p1.x = object_box.x1;
if (object_box.y1 > p1.y)
p1.y = object_box.y1;
if (object_box.z1 > p1.z)
p1.z = object_box.z1;
}
p1.x += kEpsilon; p1.y += kEpsilon; p1.z += kEpsilon;
return (p1);
}
#if 0
// The following functions read a file in PLY format, and construct mesh triangles where the data is stored
// in the mesh object
// They are just small wrapper functions that call the functions read_ply_file or read_uv_ply_file that
// do the actual reading
// These use the PLY code by Greg Turk to read the PLY file
// ----------------------------------------------------------------------------- read_flat_triangles
void
Grid::read_flat_triangles(char* file_name) {
read_ply_file(file_name, flat);
}
// ----------------------------------------------------------------------------- read_smooth_triangles
void
Grid::rea
没有合适的资源?快使用搜索试试~ 我知道了~
raytracegroundup_v1.8_TriangleMesh_20170306
共155个文件
o:51个
cpp:46个
h:46个
需积分: 31 38 下载量 61 浏览量
2017-03-06
21:36:49
上传
评论 2
收藏 1.46MB RAR 举报
温馨提示
ray_tracing ground_up triangle_mesh 用三角形网格(Triangle Mesh)分别细分球面和牛角面。 包含完整代码和对比测试图形。
资源推荐
资源详情
资源评论
收起资源包目录
raytracegroundup_v1.8_TriangleMesh_20170306 (155个子文件)
FlatSphere_160,80_269s.bmp 469KB
FlatHorn_160,80_288s.bmp 469KB
FlatSphere_16,8_299s.bmp 469KB
FlatHorn_1600,800_453s.bmp 469KB
FlatHorn_8,4_371s_from0,-2,100_at0,-2,0.bmp 469KB
FlatSphere_8,4_315s_from0,0,0_at0,0,20.bmp 469KB
FlatHorn_16,8_434s.bmp 469KB
FlatSphere_1600,800_438s.bmp 469KB
raytracegroundup.cbp 5KB
Grid.cpp 32KB
Instance.cpp 12KB
BuildShadedObjects.cpp 9KB
Sampler.cpp 9KB
Maths.cpp 5KB
Rectangle.cpp 5KB
SmoothTriangle.cpp 4KB
Triangle.cpp 4KB
Sphere.cpp 4KB
MultiJittered.cpp 4KB
OpenCylinder.cpp 4KB
Phong.cpp 3KB
AreaLight.cpp 3KB
Vector3D.cpp 3KB
Compound.cpp 3KB
GlossySpecular.cpp 3KB
World.cpp 3KB
BBox.cpp 3KB
Matte.cpp 3KB
Normal.cpp 3KB
Disk.cpp 3KB
Pinhole.cpp 3KB
Matrix.cpp 3KB
AmbientOccluder.cpp 2KB
GeometricObject.cpp 2KB
PointLight.cpp 2KB
Point3D.cpp 2KB
Camera.cpp 2KB
Plane.cpp 2KB
RGBColor.cpp 2KB
AreaLighting.cpp 1KB
Directional.cpp 1KB
Lambertian.cpp 1KB
BRDF.cpp 1KB
Ambient.cpp 1KB
RayCast.cpp 1KB
Mesh.cpp 1KB
Emissive.cpp 1KB
ViewPlane.cpp 1KB
Light.cpp 1KB
ShadeRec.cpp 1KB
Point2D.cpp 1KB
Ray.cpp 1KB
Material.cpp 1KB
Tracer.cpp 826B
main.cpp 162B
raytracegroundup.depend 36KB
raytracegroundup.exe 3.3MB
ply.h 7KB
Vector3D.h 5KB
Normal.h 4KB
RGBColor.h 4KB
Point3D.h 3KB
Camera.h 3KB
Grid.h 3KB
World.h 3KB
Maths.h 2KB
PointLight.h 2KB
Phong.h 2KB
ViewPlane.h 2KB
Sampler.h 2KB
Directional.h 2KB
GlossySpecular.h 2KB
Instance.h 2KB
Sphere.h 2KB
AmbientOccluder.h 2KB
Matte.h 2KB
AreaLight.h 2KB
GeometricObject.h 2KB
Lambertian.h 2KB
Rectangle.h 2KB
SmoothTriangle.h 1KB
Ambient.h 1KB
Emissive.h 1KB
Compound.h 1KB
Mesh.h 1KB
OpenCylinder.h 1KB
Pinhole.h 1KB
Matrix.h 1KB
ShadeRec.h 1KB
Disk.h 1KB
Plane.h 996B
Triangle.h 986B
Light.h 850B
Point2D.h 840B
BBox.h 837B
MultiJittered.h 821B
BRDF.h 813B
Tracer.h 731B
Ray.h 607B
Material.h 604B
共 155 条
- 1
- 2
资源评论
图形跟班
- 粉丝: 918
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功