int segIntersect(Point2 A, Point2 B, Point2 C, Point2 D, Point2 &InterPt) { Vector2 b, d, dp, bp, c; b.set((B.x-A.x) , (B.y-A.y)); d.set((D.x-C.x) , (D.y-C.y)); bp.set(b.y , -b.x); dp.set(d.y , -d.x); c.set((C.x-A.x) , (C.y-A.y)); float t, u, dp_b; dp_b=dp.x*b.x + dp.y*b.y; if(dp_b != 0) { t=(dp.x*c.x + dp.y*c.y)/dp_b ; u=-(bp.x*c.x + bp.y*c.y)/(bp.x*d.x + bp.y*d.y); if((t>0 && t<1) && (u>0 && u<1)) { InterPt.set((A.x+b.x*t) , (A.y+b.y*t)); return 0; } else return 1; } else { if((c.x*b.y - c.y*b.x)!=0) return -1; } }
float dotProduct(Point2 A, Point2 B) { Point2 ret; ret.set((A.getX()*B.getX()),(A.getY()*B.getY())); int r = ret.getX() + ret.getY(); return r; }
Point2 addPoints(Point2 A, Point2 B) { int x = B.getX() + A.getX(); int y = B.getY() + A.getY(); Point2 ret; ret.set(x,y); return ret; }
Point2 subtactPoints(Point2 A, Point2 B) { int x = B.getX() - A.getX(); int y = B.getY() - A.getY(); Point2 ret; ret.set(x,y); return ret; }
void lineTo(Point2 p) { glBegin(GL_LINES); glVertex2f(CP.x, CP.y); glVertex2f(p.x, p.y); glEnd(); glFlush(); CP.set(p); }
Point2 interact(Point2 A, Point2 B, Point2 C, Point2 D) { Point2 inter; float a,b,c,d,k1,k2,x,y; a=A.y-B.y; b=B.x-A.x; c=C.y-D.y; d=D.x-C.x; k1=a*A.x + b*A.y; k2=c*C.x + d*C.y; x=(d*k1 - b*k2)/(a*d - b*c); //y=(c*k1 - a*k2)/(b*c -a*d); y=( a*k2 -c*k1 )/(a*d - b*c ); inter.set(x , y); return inter; }
Point2 scalePoint(float A, Point2 B) { Point2 ret; ret.set(A*B.getX(), A*B.getY()); return ret; }
Point2 getNormal(Point2 A) { Point2 ret; ret.set(-A.getY(), A.getX()); return ret; }
void moveTo(Point2 p) { CP.set(p); }
//function that detects mouse clicks and creates the polygon void myMouseState(int button, int state, int x, int y) { if (initializeComplete == false) { if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { polyPoint[numPoints].set((float) x, (float) (WINDOW_HEIGHT-y)); ++numPoints; } else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) initializeComplete = true; glutPostRedisplay(); } else { switch (button) { case (GLUT_RIGHT_BUTTON): if (state == GLUT_DOWN) { moveEnabled = false; for (int index=0; index<numPoints; index++) { if ((x < ((int)polyPoint[index].getX() + 20)) && (x > ((int)polyPoint[index].getX() - 20)) && (y < (WINDOW_HEIGHT - (int)polyPoint[index].getY() + 20)) && (y > (WINDOW_HEIGHT - (int)polyPoint[index].getY() - 20))) { moveEnabled = true; polyIndex = index; } } } else if (state == GLUT_UP) { moveEnabled = false; if (firstPointDrawn == true) { //pointInside = true; //remove this--for test only Point2 *polyPointPtr; polyPointPtr = &polyPoint[0]; insideOutside(testPoint, polyPointPtr, numPoints); } } break; case (GLUT_LEFT_BUTTON): if (state == GLUT_DOWN) { //pointInside = false; //remove this--for test only Point2 *polyPointPtr; polyPointPtr = &polyPoint[0]; testPoint.set((float) x, (float) (WINDOW_HEIGHT-y)); firstPointDrawn = true; insideOutside(testPoint, polyPointPtr, numPoints); } break; default: break; } }
void GetRelationTable(MPMesh* pMesh, MPMesh::FaceHandle curFace, MPMesh::FaceHandle seedFace, Relation* relationSeed, unsigned nMesh, Octree<>* pOctree, Relation*& output) { // 讨论相邻的两个三角形,如何从一个三角形导出另一个三角形的关系表的问题 output = new Relation[nMesh]; memcpy(output, relationSeed, sizeof(Relation)*nMesh); if (seedFace == curFace) return; ISectTriangle* triSeed = pMesh->property(pMesh->SurfacePropHandle, seedFace); ISectTriangle* triCur = pMesh->property(pMesh->SurfacePropHandle, curFace); if (!triSeed || (!triSeed->coplanarTris.size() && !triSeed->segs.size())) { assert(triCur); MarkNARelation(triCur, output); } else { int index = TestNeighborIndex(pMesh, seedFace, curFace); assert(index != -1); Vec3d* v[3]; unsigned nv = triSeed->vertices.size(); GetCorners(pMesh, seedFace, v[0], v[1], v[2]); int a = triSeed->xi; int b = triSeed->yi; #ifdef _DEBUG Vec3d* u[3]; GetCorners(pMesh, curFace, u[0], u[1], u[2]); #endif Point2 p[3]; p[0].set((*v[0])[a], (*v[0])[b]); p[1].set((*v[1])[a], (*v[1])[b]); p[2].set((*v[2])[a], (*v[2])[b]); a = (index+1)%3; b = (index+2)%3; Point2 testPoint; testPoint.set(p[a].x()+p[b].x()-p[index].x(), p[a].y()+p[b].y()-p[index].y()); // 首先用BSP把所有的关系都弄出来 for (auto& pair: triSeed->segs) output[pair.first] = BSP2DInOutTest(pair.second.bsp, &testPoint); // 如果存在共面,那么BSP的测量是不准确的,所以置0 for (auto& pair2: triSeed->coplanarTris) { if (output[pair2.first] == REL_NOT_AVAILABLE) output[pair2.first] = REL_UNKNOWN; } // 对所有的相交元置-1 if (triCur) MarkNARelation(triCur, output); std::vector<unsigned> errorList; // 这个时候,按道理来说,所有的关系已经确定 // 但因为存在误差,可能存在为0的关系 for (auto& pair2: triSeed->coplanarTris) { if (output[pair2.first] == REL_UNKNOWN) errorList.push_back(pair2.first); } if (errorList.size()) { Vec3d *g[3]; GetCorners(pMesh, curFace, g[0], g[1], g[2]); auto bc = (*g[0]+*g[1]+*g[2])/3.0; for (auto index: errorList) output[index] = PolyhedralInclusionTest(bc, pOctree, index, pOctree->pMesh[index]->bInverse); } } }