BasicGLPane::BasicGLPane(wxFrame* parent, int* args) : wxGLCanvas(parent, wxID_ANY, args, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE) { m_context = new wxGLContext(this); // GLenum err = glewInit(); // if (GLEW_OK != err) exit(2); // To avoid flashing on MSW SetBackgroundStyle(wxBG_STYLE_CUSTOM); current_frame = 0; max_frames = 51; /* wxString caption = wxT("Choose a file"); wxString wildcard = wxT("Binary Dump (*.bin)|*.bin"); wxString defaultDir = wxT("C:\\dev\\1\\run_20121006_20-06-21.996857_5x5_SBCGDIAG"); wxString defaultFilename = wxEmptyString; wxFileDialog dialog(NULL, caption, defaultDir, defaultFilename, wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST ); dialog.CentreOnParent(); std::string filename(""); if(dialog.ShowModal() == wxID_OK) { auto a = dialog.GetPath(); filename.append(a.c_str()); } else exit(1); */ #define MINMAXDECL(ARG) max_##ARG = -DBL_MAX; min_##ARG = DBL_MAX; MINMAXDECL(sigma0); MINMAXDECL(x); MINMAXDECL(y); const char* filename_start("C:\\dev\\1\\run_20121006_20-06-21.996857_5x5_SBCGDIAG\\00"); const char* filename_end(".00.dump.bin"); for (int i = 0; i < max_frames; ++i) { std::stringstream filename; filename << filename_start; if (i < 10) filename << 0; filename << i; filename << filename_end; std::string act_filename(filename.str()); std::vector<finite_el_t> buff; std::fstream ifs(act_filename, std::ios_base::in | std::ios_base::binary); //std::cout << filename << std::endl; u64 filesize; ifs.read(reinterpret_cast<char*>(&filesize),sizeof(u64)); std::cout << "Read " << std::hex << filesize << std::endl; ifs.seekg(filesize, std::ios_base::beg); u64 element_number; ifs.read(reinterpret_cast<char*>(&element_number),sizeof(u64)); std::cout << "Read " << std::hex << element_number << std::endl; buff.reserve(element_number); ifs.seekg(sizeof(u64), std::ios_base::beg); #define FINDMINMAX(ARG, PATH) if (PATH > max_##ARG) max_##ARG = PATH; if (PATH < min_##ARG) min_##ARG = PATH; for (size_t i = 0; i < element_number; ++i) { finite_el_t el; ifs.read(reinterpret_cast<char*>(&el),sizeof(finite_el_t)); for (size_t i = 0; i < KUZLOV; ++i) { FINDMINMAX(sigma0, el.nds[i].sigma0) FINDMINMAX(x, el.el.x[i]); FINDMINMAX(y, el.el.y[i]); } buff.push_back(el); } ifs.close(); buf.push_back(buff); } }
void RawSlabsPlugin::findMinMax() { QProgressDialog progress("Finding Min and Max", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = m_width*m_height*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; for (int nf=0; nf<m_fileName.count(); nf++) { QFile fin(m_fileName[nf]); fin.open(QFile::ReadOnly); fin.seek(m_skipBytes); m_rawMin = 10000000; m_rawMax = -10000000; int istart = ((nf > 0) ? m_slices[nf-1] : 0); int iend = m_slices[nf]; for(int i=istart; i<iend; i++) { progress.setValue((int)(100.0*(float)i/(float)m_depth)); qApp->processEvents(); fin.read((char*)tmp, nbytes); if (m_voxelType == _UChar) { uchar *ptr = tmp; FINDMINMAX(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; FINDMINMAX(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; FINDMINMAX(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; FINDMINMAX(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; FINDMINMAX(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; FINDMINMAX(); } } fin.close(); } delete [] tmp; progress.setValue(100); qApp->processEvents(); }
int triBoxOverlap(float boxcenter[3],float boxhalfsize[3],float triverts[3][3]) { /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ float v0[3],v1[3],v2[3]; // float axis[3]; float min,max,p0,p1,p2,rad,fex,fey,fez; // -NJMP- "d" local variable removed float normal[3],e0[3],e1[3],e2[3]; /* This is the fastest branch on Sun */ /* move everything so that the boxcenter is in (0,0,0) */ SUB(v0,triverts[0],boxcenter); SUB(v1,triverts[1],boxcenter); SUB(v2,triverts[2],boxcenter); /* compute triangle edges */ SUB(e0,v1,v0); /* tri edge 0 */ SUB(e1,v2,v1); /* tri edge 1 */ SUB(e2,v0,v2); /* tri edge 2 */ /* Bullet 3: */ /* test the 9 tests first (this was faster) */ fex = fabsf(e0[X]); fey = fabsf(e0[Y]); fez = fabsf(e0[Z]); AXISTEST_X01(e0[Z], e0[Y], fez, fey); AXISTEST_Y02(e0[Z], e0[X], fez, fex); AXISTEST_Z12(e0[Y], e0[X], fey, fex); fex = fabsf(e1[X]); fey = fabsf(e1[Y]); fez = fabsf(e1[Z]); AXISTEST_X01(e1[Z], e1[Y], fez, fey); AXISTEST_Y02(e1[Z], e1[X], fez, fex); AXISTEST_Z0(e1[Y], e1[X], fey, fex); fex = fabsf(e2[X]); fey = fabsf(e2[Y]); fez = fabsf(e2[Z]); AXISTEST_X2(e2[Z], e2[Y], fez, fey); AXISTEST_Y1(e2[Z], e2[X], fez, fex); AXISTEST_Z12(e2[Y], e2[X], fey, fex); /* Bullet 1: */ /* first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X-direction */ FINDMINMAX(v0[X],v1[X],v2[X],min,max); if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0; /* test in Y-direction */ FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max); if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0; /* test in Z-direction */ FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max); if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0; /* Bullet 2: */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ CROSS(normal,e0,e1); // -NJMP- (line removed here) if(!planeBoxOverlap(normal,v0,boxhalfsize)) return 0; // -NJMP- return 1; /* box and triangle overlaps */ }
static bool fast_tri_box_overlap(const Vector3& boxcenter,const Vector3 boxhalfsize,const Vector3 *triverts) { /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ Vector3 v0,v1,v2; float min,max,d,p0,p1,p2,rad,fex,fey,fez; Vector3 normal,e0,e1,e2; /* This is the fastest branch on Sun */ /* move everything so that the boxcenter is in (0,0,0) */ v0=triverts[0]-boxcenter; v1=triverts[1]-boxcenter; v2=triverts[2]-boxcenter; /* compute triangle edges */ e0=v1-v0; /* tri edge 0 */ e1=v2-v1; /* tri edge 1 */ e2=v0-v2; /* tri edge 2 */ /* Bullet 3: */ /* test the 9 tests first (this was faster) */ fex = Math::abs(e0.x); fey = Math::abs(e0.y); fez = Math::abs(e0.z); AXISTEST_X01(e0.z, e0.y, fez, fey); AXISTEST_Y02(e0.z, e0.x, fez, fex); AXISTEST_Z12(e0.y, e0.x, fey, fex); fex = Math::abs(e1.x); fey = Math::abs(e1.y); fez = Math::abs(e1.z); AXISTEST_X01(e1.z, e1.y, fez, fey); AXISTEST_Y02(e1.z, e1.x, fez, fex); AXISTEST_Z0(e1.y, e1.x, fey, fex); fex = Math::abs(e2.x); fey = Math::abs(e2.y); fez = Math::abs(e2.z); AXISTEST_X2(e2.z, e2.y, fez, fey); AXISTEST_Y1(e2.z, e2.x, fez, fex); AXISTEST_Z12(e2.y, e2.x, fey, fex); /* Bullet 1: */ /* first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X-direction */ FINDMINMAX(v0.x,v1.x,v2.x,min,max); if(min>boxhalfsize.x || max<-boxhalfsize.x) return false; /* test in Y-direction */ FINDMINMAX(v0.y,v1.y,v2.y,min,max); if(min>boxhalfsize.y || max<-boxhalfsize.y) return false; /* test in Z-direction */ FINDMINMAX(v0.z,v1.z,v2.z,min,max); if(min>boxhalfsize.z || max<-boxhalfsize.z) return false; /* Bullet 2: */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ normal=e0.cross(e1); d=-normal.dot(v0); /* plane eq: normal.x+d=0 */ if(!planeBoxOverlap(normal,d,boxhalfsize)) return false; return true; /* box and triangle overlaps */ }
bool Octree::boxTriangleIntersection(BoundingBox Box, Triangle triangle) { //This algorithm is a composition of 13 tests to determine intersection //if all pass, there is overlap float min,max,d,p0,p1,p2,rad,fex,fey,fez; //center the box at (0,0,0) with respect to the triangle //translate triangle vertices Vector v0,v1,v2; //edges of new triangle Vector e0,e1,e2; //Vector that goes half the diagonal of the bounding box Vector boxHalfSize = (Box.boxMaxExtent - Box.boxMinExtent) / 2.0; point boxCenter( (Box.boxMinExtent.px + Box.boxMaxExtent.px)/2.0, (Box.boxMinExtent.py + Box.boxMaxExtent.py)/2.0, (Box.boxMinExtent.pz + Box.boxMaxExtent.pz)/2.0 ); /*v0.set(triangle.Vertex[0].px-boxCenter.px,triangle.Vertex[0].py-boxCenter.py,triangle.Vertex[0].pz-boxCenter.pz); v1.set(triangle.Vertex[1].px-boxCenter.px,triangle.Vertex[1].py-boxCenter.py,triangle.Vertex[1].pz-boxCenter.pz); v2.set(triangle.Vertex[2].px-boxCenter.px,triangle.Vertex[2].py-boxCenter.py,triangle.Vertex[2].pz-boxCenter.pz);*/ v0 = triangle.Vertex[0] - boxCenter; v1 = triangle.Vertex[1] - boxCenter; v2 = triangle.Vertex[2] - boxCenter; e0 = v1 - v0; e1 = v2 - v1; e2 = v0 - v2; //the first 9 tests fex = fabs(e0.px); fey = fabs(e0.py); fez = fabs(e0.pz); AXISTEST_X01(e0.pz, e0.py, fez, fey); AXISTEST_Y02(e0.pz, e0.px, fez, fex); AXISTEST_Z12(e0.py, e0.px, fey, fex); fex = fabs(e1.px); fey = fabs(e1.py); fez = fabs(e1.pz); AXISTEST_X01(e1.pz, e1.py, fez, fey); AXISTEST_Y02(e1.pz, e1.px, fez, fex); AXISTEST_Z0(e1.py, e1.px, fey, fex); fex = fabs(e2.px); fey = fabs(e2.py); fez = fabs(e2.pz); AXISTEST_X2(e2.pz, e2.py, fez, fey); AXISTEST_Y1(e2.pz, e2.px, fez, fex); AXISTEST_Z12(e2.py, e2.px, fey, fex); //now the 3 tests //X test FINDMINMAX(v0.px,v1.px,v2.px,min,max); if(min>boxHalfSize.px || max<-boxHalfSize.px) return false; //Y test FINDMINMAX(v0.py,v1.py,v2.py,min,max); if(min>boxHalfSize.py || max<-boxHalfSize.py) return false; //Z test FINDMINMAX(v0.pz,v1.pz,v2.pz,min,max); if(min>boxHalfSize.pz || max<-boxHalfSize.pz) return false; Vector normal = e0.crossProduct(e1); d = -(normal.dotProduct(v0)); if(!planeBoxOverlap(normal,d,boxHalfSize)) return false; return true; }
bool8 AtiTriBoxMoller( const TBM_FLOAT boxcenter[3], const TBM_FLOAT boxhalfsize[3], const TBM_FLOAT triVert0[3], const TBM_FLOAT triVert1[3], const TBM_FLOAT triVert2[3]) { // use separating axis theorem to test overlap between triangle and box // need to test for overlap in these directions: // 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle // we do not even need to test these) // 2) normal of the triangle // 3) crossproduct(edge from tri, {x,y,z}-directin) // this gives 3x3=9 more tests TBM_FLOAT v0[3], v1[3], v2[3]; TBM_FLOAT min, max, d, p0, p1, p2, rad, fex, fey, fez; TBM_FLOAT normal[3], e0[3], e1[3], e2[3]; // This is the fastest branch on Sun // move everything so that the boxcenter is in (0,0,0) SUB (v0, triVert0, boxcenter); SUB (v1, triVert1, boxcenter); SUB (v2, triVert2, boxcenter); // compute triangle edges SUB (e0, v1, v0); // tri edge 0 SUB (e1, v2, v1); // tri edge 1 SUB (e2, v0, v2); // tri edge 2 // Bullet 3: // test the 9 tests first (this was faster) fex = (TBM_FLOAT)fabs (e0[X]); fey = (TBM_FLOAT)fabs (e0[Y]); fez = (TBM_FLOAT)fabs (e0[Z]); AXISTEST_X01 (e0[Z], e0[Y], fez, fey); AXISTEST_Y02 (e0[Z], e0[X], fez, fex); AXISTEST_Z12 (e0[Y], e0[X], fey, fex); fex = (TBM_FLOAT)fabs (e1[X]); fey = (TBM_FLOAT)fabs (e1[Y]); fez = (TBM_FLOAT)fabs (e1[Z]); AXISTEST_X01 (e1[Z], e1[Y], fez, fey); AXISTEST_Y02 (e1[Z], e1[X], fez, fex); AXISTEST_Z0 (e1[Y], e1[X], fey, fex); fex = (TBM_FLOAT)fabs (e2[X]); fey = (TBM_FLOAT)fabs (e2[Y]); fez = (TBM_FLOAT)fabs (e2[Z]); AXISTEST_X2 (e2[Z], e2[Y], fez, fey); AXISTEST_Y1 (e2[Z], e2[X], fez, fex); AXISTEST_Z12 (e2[Y], e2[X], fey, fex); // Bullet 1: // first test overlap in the {x,y,z}-directions // find min, max of the triangle each direction, and test for overlap in // that direction -- this is equivalent to testing a minimal AABB around // the triangle against the AABB // test in X-direction FINDMINMAX (v0[X], v1[X], v2[X], min, max); if (min > boxhalfsize[X] || max < -boxhalfsize[X]) { return FALSE; } // test in Y-direction FINDMINMAX (v0[Y], v1[Y], v2[Y], min, max); if (min > boxhalfsize[Y] || max < -boxhalfsize[Y]) { return FALSE; } // test in Z-direction FINDMINMAX (v0[Z], v1[Z], v2[Z], min, max); if (min > boxhalfsize[Z] || max < -boxhalfsize[Z]) { return FALSE; } // Bullet 2: // test if the box intersects the plane of the triangle // compute plane equation of triangle: normal*x+d=0 CROSS (normal, e0, e1); d = -DOT (normal, v0); // plane eq: normal.x+d=0 return AtiPlaneBoxOverlap (normal, d, boxhalfsize); }
void RawPlugin::findMinMax() { QProgressDialog progress("Finding Min and Max", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; QFile fin(m_fileName[0]); fin.open(QFile::ReadOnly); fin.seek(m_skipBytes); m_rawMin = 10000000; m_rawMax = -10000000; for(int i=0; i<nX; i++) { progress.setValue((int)(100.0*(float)i/(float)nX)); qApp->processEvents(); fin.read((char*)tmp, nbytes); if (m_voxelType == _UChar) { uchar *ptr = tmp; FINDMINMAX(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; FINDMINMAX(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; FINDMINMAX(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; FINDMINMAX(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; FINDMINMAX(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; FINDMINMAX(); } } fin.close(); delete [] tmp; progress.setValue(100); qApp->processEvents(); }
/* by Tomas Akenine-Möller */ bool BoundingBox::containsTriangle( const Vec& tv0, const Vec& tv1, const Vec& tv2 ) const { Vec boxcenter(center); Vec boxhalfsize(xExtent, yExtent, zExtent); int X = 0, Y = 1, Z = 2; /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ Vec v0,v1,v2; double min,max,p0,p1,p2,rad,fex,fey,fez; Vec normal,e0,e1,e2; /* This is the fastest branch on Sun */ /* move everything so that the box center is in (0,0,0) */ v0=tv0-boxcenter; v1=tv1-boxcenter; v2=tv2-boxcenter; /* compute triangle edges */ e0=v1-v0; /* tri edge 0 */ e1=v2-v1; /* tri edge 1 */ e2=v0-v2; /* tri edge 2 */ /* Bullet 3: */ /* test the 9 tests first (this was faster) */ fex = fabsf(e0[X]); fey = fabsf(e0[Y]); fez = fabsf(e0[Z]); AXISTEST_X01(e0[Z], e0[Y], fez, fey); AXISTEST_Y02(e0[Z], e0[X], fez, fex); AXISTEST_Z12(e0[Y], e0[X], fey, fex); fex = fabsf(e1[X]); fey = fabsf(e1[Y]); fez = fabsf(e1[Z]); AXISTEST_X01(e1[Z], e1[Y], fez, fey); AXISTEST_Y02(e1[Z], e1[X], fez, fex); AXISTEST_Z0(e1[Y], e1[X], fey, fex); fex = fabsf(e2[X]); fey = fabsf(e2[Y]); fez = fabsf(e2[Z]); AXISTEST_X2(e2[Z], e2[Y], fez, fey); AXISTEST_Y1(e2[Z], e2[X], fez, fex); AXISTEST_Z12(e2[Y], e2[X], fey, fex); /* Bullet 1: */ /* first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X-direction */ FINDMINMAX(v0[X],v1[X],v2[X],min,max); if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0; /* test in Y-direction */ FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max); if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0; /* test in Z-direction */ FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max); if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0; /* Bullet 2: */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ normal = e0 ^ e1; if(!planeBoxOverlap(normal,v0,boxhalfsize)) return 0; return 1; /* box and triangle overlaps */ }
// main function for triangle box overlaping bool triBoxOverlap(Vector4 boxcenter, float boxhalfsize, Vector4 p0, Vector4 p1, Vector4 p2) { /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ Vector4 v0,v1,v2; Vector4 e0,e1,e2; Vector4 normal; float min,max,fex,fey,fez; /* This is the fastest branch on Sun */ /* move everything so that the boxcenter is in (0,0,0) */ v0 = p0 - boxcenter; v1 = p1 - boxcenter; v2 = p2 - boxcenter; /* compute triangle edges */ e0 = v1 - v0; /* tri edge 0 */ e1 = v2 - v1; /* tri edge 1 */ e2 = v0 - v2; /* tri edge 2 */ /* Bullet 3: */ /* test the 9 tests first (this was faster) */ fex = fabs(e0.X); fey = fabs(e0.Y); fez = fabs(e0.Z); if (!AXISTEST(e0.Z, e0.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v2.Y, v2.Z)) return false; if (!AXISTEST(e0.Z, e0.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v2.X, -v2.Z)) return false; if (!AXISTEST(e0.Y, e0.X, fey, fex, boxhalfsize, v1.X, v1.Y, v2.X, v2.Y)) return false; fex = fabs(e1.X); fey = fabs(e1.Y); fez = fabs(e1.Z); if (!AXISTEST(e1.Z, e1.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v2.Y, v2.Z)) return false; if (!AXISTEST(e1.Z, e1.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v2.X, -v2.Z)) return false; if (!AXISTEST(e1.Y, e1.X, fey, fex, boxhalfsize, v0.X, v0.Y, v1.X, v1.Y)) return false; fex = fabs(e2.X); fey = fabs(e2.Y); fez = fabs(e2.Z); if (!AXISTEST(e2.Z, e2.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v1.Y, v1.Z)) return false; if (!AXISTEST(e2.Z, e2.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v1.X, -v1.Z)) return false; if (!AXISTEST(e2.Y, e2.X, fey, fex, boxhalfsize, v1.X, v1.Y, v2.X, v2.Y)) return false; /* Bullet 1: */ /* first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X-direction */ FINDMINMAX(v0.X,v1.X,v2.X,min,max); if(min>boxhalfsize || max<-boxhalfsize) return false; /* test in Y-direction */ FINDMINMAX(v0.Y,v1.Y,v2.Y,min,max); if(min>boxhalfsize || max<-boxhalfsize) return false; /* test in Z-direction */ FINDMINMAX(v0.Z,v1.Z,v2.Z,min,max); if(min>boxhalfsize || max<-boxhalfsize) return false; /* Bullet 2: */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ normal = e0 % e1; if(!planeBoxOverlap(normal,v0,boxhalfsize)) return false; return true; /* box and triangle overlaps */ }
int IsTriangleIntersectBox(float boxcenter[3], float boxhalf[3], float triverts[3][3]) { /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ float v0[3],v1[3],v2[3]; float min,max,d,p0,p1,p2,rad,fex,fey,fez; float normal[3],e0[3],e1[3],e2[3]; /* 1) first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X */ v0[X]=triverts[0][X]-boxcenter[X]; v1[X]=triverts[1][X]-boxcenter[X]; v2[X]=triverts[2][X]-boxcenter[X]; FINDMINMAX(v0[X],v1[X],v2[X],min,max); if(min>boxhalf[X] || max<-boxhalf[X]) return 0; /* test in Y */ v0[Y]=triverts[0][Y]-boxcenter[Y]; v1[Y]=triverts[1][Y]-boxcenter[Y]; v2[Y]=triverts[2][Y]-boxcenter[Y]; FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max); if(min>boxhalf[Y] || max<-boxhalf[Y]) return 0; /* test in Z */ v0[Z]=triverts[0][Z]-boxcenter[Z]; v1[Z]=triverts[1][Z]-boxcenter[Z]; v2[Z]=triverts[2][Z]-boxcenter[Z]; FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max); if(min>boxhalf[Z] || max<-boxhalf[Z]) return 0; /* 2) */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ SUB(e0,v1,v0); /* tri edge 0 */ SUB(e1,v2,v1); /* tri edge 1 */ CROSS(normal,e0,e1); d=-DOT(normal,v0); /* plane eq: normal.x+d=0 */ if(!planeBoxOverlap(normal,d,boxhalf)) return 0; /* compute the last triangle edge */ SUB(e2,v0,v2); /* 3) */ fex = fabsf(e0[X]); fey = fabsf(e0[Y]); fez = fabsf(e0[Z]); AXISTEST_X01(e0[Z], e0[Y], fez, fey); AXISTEST_Y02(e0[Z], e0[X], fez, fex); AXISTEST_Z12(e0[Y], e0[X], fey, fex); fex = fabsf(e1[X]); fey = fabsf(e1[Y]); fez = fabsf(e1[Z]); AXISTEST_X01(e1[Z], e1[Y], fez, fey); AXISTEST_Y02(e1[Z], e1[X], fez, fex); AXISTEST_Z0(e1[Y], e1[X], fey, fex); fex = fabsf(e2[X]); fey = fabsf(e2[Y]); fez = fabsf(e2[Z]); AXISTEST_X2(e2[Z], e2[Y], fez, fey); AXISTEST_Y1(e2[Z], e2[X], fez, fex); AXISTEST_Z12(e2[Y], e2[X], fey, fex); return 1; }
void NrrdPlugin::findMinMax() { QProgressDialog progress("Finding Min and Max", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; int idx[3]; int sz[3]; idx[0] = idx[1] = idx[2] = 0; sz[0] = m_height; sz[1] = m_width; sz[2] = 1; m_rawMin = 10000000; m_rawMax = -10000000; for(int i=0; i<nX; i++) { progress.setValue((int)(100.0*(float)i/(float)nX)); qApp->processEvents(); idx[2] = i; if (m_voxelType == _UChar) readSlice<unsigned char>(idx, sz, nbytes, tmp); else if (m_voxelType == _Char) readSlice<char>(idx, sz, nbytes, tmp); else if (m_voxelType == _UShort) readSlice<unsigned short>(idx, sz, nbytes, tmp); else if (m_voxelType == _Short) readSlice<short>(idx, sz, nbytes, tmp); else if (m_voxelType == _Int) readSlice<int>(idx, sz, nbytes, tmp); else if (m_voxelType == _Float) readSlice<float>(idx, sz, nbytes, tmp); if (m_voxelType == _UChar) { uchar *ptr = tmp; FINDMINMAX(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; FINDMINMAX(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; FINDMINMAX(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; FINDMINMAX(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; FINDMINMAX(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; FINDMINMAX(); } } delete [] tmp; progress.setValue(100); qApp->processEvents(); }
int IntersectionTests::triBoxOverlap(float boxhalfsize[3],float v0[3], float v1[3],float v2[3]) { /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ /* we do not even need to test these) */ /* 2) normal of the triangle */ /* 3) crossproduct(edge from tri, {x,y,z}-directin) */ /* this gives 3x3=9 more tests */ //double v0[3],v1[3],v2[3]; // double axis[3]; float min,max,d,p0,p1,p2,rad,fex,fey,fez; float normal[3],e0[3],e1[3],e2[3]; /* This is the fastest branch on Sun */ /* move everything so that the boxcenter is in (0,0,0) */ // SUB(v0,trivertsA,boxcenter); // SUB(v1,trivertsB,boxcenter); // SUB(v2,trivertsC,boxcenter); /* compute triangle edges */ //e0[0] = v1[0] - v0[0]; e0[1] = v1[1] - v0[1]; e0[2] = v1[2] - v0[2]; //e1[0] = v2[0] - v1[0]; e1[1] = v2[1] - v1[1]; e1[2] = v2[2] - v1[2]; //e2[0] = v0[0] - v2[0]; e2[1] = v0[1] - v2[1]; e2[2] = v0[2] - v2[2]; SUB(e0,v1,v0); /* tri edge 0 */ SUB(e1,v2,v1); /* tri edge 1 */ SUB(e2,v0,v2); /* tri edge 2 */ /* Bullet 3: */ /* test the 9 tests first (this was faster) */ fex = e0[X];//fabs(e0[X]); fey = e0[Y];//fabs(e0[Y]); fez = e0[Z];//fabs(e0[Z]); if(e0[X] < 0) fex = -e0[X]; if(e0[Y] < 0) fey = -e0[Y]; if(e0[Z] < 0) fez = -e0[Z]; AXISTEST_X01(e0[Z], e0[Y], fez, fey); AXISTEST_Y02(e0[Z], e0[X], fez, fex); AXISTEST_Z12(e0[Y], e0[X], fey, fex); fex = e1[X];//fabs(e1[X]); fey = e1[Y];//fabs(e1[Y]); fez = e1[Z];//fabs(e1[Z]); if(e1[X] < 0) fex = -e1[X]; if(e1[Y] < 0) fey = -e1[Y]; if(e1[Z] < 0) fez = -e1[Z]; AXISTEST_X01(e1[Z], e1[Y], fez, fey); AXISTEST_Y02(e1[Z], e1[X], fez, fex); AXISTEST_Z0(e1[Y], e1[X], fey, fex); fex = e2[X];//fabs(e2[X]); fey = e2[Y];//fabs(e2[Y]); fez = e2[Z];//fabs(e2[Z]); if(e2[X] < 0) fex = -e2[X]; if(e2[Y] < 0) fey = -e2[Y]; if(e2[Z] < 0) fez = -e2[Z]; AXISTEST_X2(e2[Z], e2[Y], fez, fey); AXISTEST_Y1(e2[Z], e2[X], fez, fex); AXISTEST_Z12(e2[Y], e2[X], fey, fex); /* Bullet 1: */ /* first test overlap in the {x,y,z}-directions */ /* find min, max of the triangle each direction, and test for overlap in */ /* that direction -- this is equivalent to testing a minimal AABB around */ /* the triangle against the AABB */ /* test in X-direction */ FINDMINMAX(v0[X],v1[X],v2[X],min,max); if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0; /* test in Y-direction */ FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max); if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0; /* test in Z-direction */ FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max); if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0; /* Bullet 2: */ /* test if the box intersects the plane of the triangle */ /* compute plane equation of triangle: normal*x+d=0 */ CROSS(normal,e0,e1); d=-DOT(normal,v0); /* plane eq: normal.x+d=0 */ if(!planeBoxOverlap(normal,d,boxhalfsize)) return 0; return 1; /* box and triangle overlaps */ }
// Use separating axis theorem to test overlap between triangle and box // need to test for overlap in these directions: // 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle // we do not even need to test these) // 2) normal of the triangle // 3) crossproduct(edge from tri, {x,y,z}-directin) // this gives 3x3=9 more tests bool overlapTriangleBox(Vec3r& boxcenter, Vec3r& boxhalfsize, Vec3r triverts[3]) { Vec3r v0, v1, v2, normal, e0, e1, e2; real min, max, d, p0, p1, p2, rad, fex, fey, fez; // This is the fastest branch on Sun // move everything so that the boxcenter is in (0, 0, 0) v0 = triverts[0] - boxcenter; v1 = triverts[1] - boxcenter; v2 = triverts[2] - boxcenter; // compute triangle edges e0 = v1 - v0; e1 = v2 - v1; e2 = v0 - v2; // Bullet 3: // Do the 9 tests first (this was faster) fex = fabs(e0[X]); fey = fabs(e0[Y]); fez = fabs(e0[Z]); AXISTEST_X01(e0[Z], e0[Y], fez, fey); AXISTEST_Y02(e0[Z], e0[X], fez, fex); AXISTEST_Z12(e0[Y], e0[X], fey, fex); fex = fabs(e1[X]); fey = fabs(e1[Y]); fez = fabs(e1[Z]); AXISTEST_X01(e1[Z], e1[Y], fez, fey); AXISTEST_Y02(e1[Z], e1[X], fez, fex); AXISTEST_Z0(e1[Y], e1[X], fey, fex); fex = fabs(e2[X]); fey = fabs(e2[Y]); fez = fabs(e2[Z]); AXISTEST_X2(e2[Z], e2[Y], fez, fey); AXISTEST_Y1(e2[Z], e2[X], fez, fex); AXISTEST_Z12(e2[Y], e2[X], fey, fex); // Bullet 1: // first test overlap in the {x,y,z}-directions // find min, max of the triangle each direction, and test for overlap in // that direction -- this is equivalent to testing a minimal AABB around // the triangle against the AABB // test in X-direction FINDMINMAX(v0[X], v1[X], v2[X], min, max); if (min > boxhalfsize[X] || max < -boxhalfsize[X]) return false; // test in Y-direction FINDMINMAX(v0[Y], v1[Y], v2[Y], min, max); if (min > boxhalfsize[Y] || max < -boxhalfsize[Y]) return false; // test in Z-direction FINDMINMAX(v0[Z], v1[Z], v2[Z], min, max); if(min > boxhalfsize[Z] || max < -boxhalfsize[Z]) return false; // Bullet 2: // test if the box intersects the plane of the triangle // compute plane equation of triangle: normal * x + d = 0 normal = e0 ^ e1; d = -(normal * v0); // plane eq: normal.x + d = 0 if (!overlapPlaneBox(normal, d, boxhalfsize)) return false; return true; // box and triangle overlaps }
void RemapHDF4::findMinMax() { QProgressDialog progress("Finding Min and Max", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; m_rawMin = 10000000; m_rawMax = -10000000; int32 start[2], edges[2]; start[0] = start[1] = 0; edges[0] = m_width; edges[1] = m_height; for(uint i=0; i<m_depth; i++) { progress.setValue((int)(100.0*(float)i/(float)nX)); qApp->processEvents(); int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (void*)tmp); status = SDendaccess(sds_id); status = SDend(sd_id); if (m_voxelType == _UChar) { uchar *ptr = tmp; FINDMINMAX(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; FINDMINMAX(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; FINDMINMAX(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; FINDMINMAX(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; FINDMINMAX(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; FINDMINMAX(); } } delete [] tmp; progress.setValue(100); qApp->processEvents(); }