bool PaintAggregator::shouldInvalidateScrollRect(const IntRect& rect) const { if (!rect.isEmpty()) { if (!m_update.scrollRect.intersects(rect)) return false; if (!m_update.scrollRect.contains(rect)) return true; } // Check if the combined area of all contained paint rects plus this new // rect comes too close to the area of the scrollRect. If so, then we // might as well invalidate the scroll rect. int paintArea = calculateArea(rect); for (size_t i = 0; i < m_update.paintRects.size(); ++i) { const IntRect& existingRect = m_update.paintRects[i]; if (m_update.scrollRect.contains(existingRect)) paintArea += calculateArea(existingRect); } int scrollArea = calculateArea(m_update.scrollRect); if (float(paintArea) / float(scrollArea) > maxRedundantPaintToScrollArea) return true; return false; }
void Circle::setRadius(double rad) { // radius should not be negative radius = (rad < 0) ? 0 : rad; // now recalculate area and perimeter calculateArea(); calculatePerimeter(); }
//constructor Rectangle::Rectangle(int x,int y) { height=y; width=x; calculateArea(); calculatePerimeter(); }
bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::BOX)); cpVect wh = PhysicsHelper::size2cpv(size); cpVect vec[4] = { {-wh.x/2.0f, -wh.y/2.0f}, {-wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, -wh.y/2.0f} }; cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), 4, vec, PhysicsHelper::point2cpv(offset)); CC_BREAK_IF(shape == nullptr); _info->add(shape); _offset = offset; _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN)); cpVect* vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), count, vecs, PhysicsHelper::point2cpv(offset)); CC_SAFE_DELETE_ARRAY(vecs); CC_BREAK_IF(shape == nullptr); _info->add(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); _center = PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); setMaterial(material); return true; } while (false); return false; }
bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset /*= Vec2(0, 0)*/, float radius/* = 0.0f*/) { do { _type = Type::BOX; auto wh = PhysicsHelper::size2cpv(size); cpVect vec[4] = { {-wh.x/2.0f, -wh.y/2.0f}, {-wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, wh.y/2.0f}, {wh.x/2.0f, -wh.y/2.0f} }; cpTransform transform = cpTransformTranslate(PhysicsHelper::point2cpv(offset)); auto shape = cpPolyShapeNew(s_sharedBody, 4, vec, transform, radius); CC_BREAK_IF(shape == nullptr); cpShapeSetUserData(shape, this); addShape(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
/************************ * FUNCTION DEFINITIONS * ************************/ int main(int argc, char **argv) { int numRects = 10; double area = 0.0; int myRank = 0; int numProcs = 1; int myNumRects = 0; int myDispl = 0; setupMPI(&argc, &argv, &myRank, &numProcs); getUserOptions(argc, argv, &numRects); distributeWork(numRects, myRank, numProcs, &myNumRects, &myDispl); calculateArea(numRects, myNumRects, (1.0 / numRects), myDispl, &area); syncData(myRank, numProcs, &area); if (myRank == 0) { calculateAndPrintPi(area); } MPI_Finalize(); return 0; }
bool PhysicsShapePolygon::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/, float radius/* = 0.0f*/) { do { _type = Type::POLYGEN; auto vecs = new (std::nothrow) cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); //count = cpConvexHull((int)count, vecs, nullptr, nullptr, 0); cpTransform transform = cpTransformTranslate(PhysicsHelper::point2cpv(offset)); auto shape = cpPolyShapeNew(s_sharedBody, count, vecs, transform, radius); CC_SAFE_DELETE_ARRAY(vecs); CC_BREAK_IF(shape == nullptr); cpShapeSetUserData(shape, this); addShape(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
bool PhysicsShapePolygon::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset/* = Vec2(0, 0)*/) { do { _type = Type::POLYGEN; auto vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); auto shape = cpPolyShapeNew(s_sharedBody, count, vecs, PhysicsHelper::point2cpv(offset)); CC_SAFE_DELETE_ARRAY(vecs); CC_BREAK_IF(shape == nullptr); addShape(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
void Rect::setDimensions(double l, double h) { // we do not want to accept negative values // for length and width length = (l < 0) ? 0 : l; height = (h < 0) ? 0 : h; // now update area and perimeter calculateArea(); calculatePerimeter(); }
void PaintAggregator::popPendingUpdate(PendingUpdate* update) { // Combine paint rects if their combined area is not sufficiently less than // the area of the union of all paint rects. We skip this if there is a // scroll rect since scrolling benefits from smaller paint rects. if (m_update.scrollRect.isEmpty() && m_update.paintRects.size() > 1) { int paintArea = 0; IntRect unionRect; for (size_t i = 0; i < m_update.paintRects.size(); ++i) { paintArea += calculateArea(m_update.paintRects[i]); unionRect.unite(m_update.paintRects[i]); } int unionArea = calculateArea(unionRect); if (float(paintArea) / float(unionArea) > maxPaintRectsAreaRatio) combinePaintRects(); } *update = m_update; clearPendingUpdate(); }
// // An divide & conquer method. // Use the minimal height as the divider, recursively solve the subproblems. // Worst case: T(n) = T(n-1)+O(n) -> O(n^2) // Normal case: T(n) = T(n/2)+O(n) -> O(nlgn) // int calculateArea(vector<int> &height, int start, int last) { if (start > last) return 0; int valleyHeight = INT_MAX; int valleyIndex = -1; for (int i = start; i <= last; ++i) { if (height[i] <= valleyHeight) { valleyHeight = height[i]; valleyIndex = i; } } int area1 = valleyHeight * (last-start+1); int area2 = calculateArea(height, start, valleyIndex-1); int area3 = calculateArea(height, valleyIndex+1, last); return std::max(std::max(area1, area2), area3); }
/************************ * FUNCTION DEFINITIONS * ************************/ int main(int argc, char **argv) { int numRects = 10; double area = 0.0; getUserOptions(argc, argv, &numRects); calculateArea(numRects, (1.0 / numRects), &area); calculateAndPrintPi(area); return 0; }
bool Triangulator::triangulateConcave(const he::PrimitiveList<vec2>& vertices, he::PrimitiveList<uint32>& indices) { int n = (int)vertices.size(); int* V = HENewArray(int, n); // TODO: seeb cache this buffer as a member, enlarge if needed if ( 0.0f < calculateArea(vertices) ) for (int v=0; v<n; v++) V[v] = v; else for(int v=0; v<n; v++) V[v] = (n-1)-v; int nv = n; int count = 2*nv; for(int m=0, v=nv-1; nv>2; ) { if (0 >= (count--)) { HEDeleteArray(V); return false; } int u = v ; if (nv <= u) u = 0; v = u+1; if (nv <= v) v = 0; int w = v+1; if (nv <= w) w = 0; if ( snip(vertices,u,v,w,nv,V) ) { int a,b,c,s,t; a = V[u]; b = V[v]; c = V[w]; indices.add(a); indices.add(c); indices.add(b); m++; for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--; count = 2*nv; } } HEDeleteArray(V); return true; }
/************************ * FUNCTION DEFINITIONS * ************************/ int main(int argc, char **argv) { long numRects = 1e9; double area = 0.0; std::cout << "NumRects being used = " << numRects << std::endl; if(CUDA_ENABLED){ printf("CUDA is enabled\n"); } else{ printf("Cuda is not enabled\n"); } calculateArea(numRects, &area); std::cout << "Pi = " << 4*area << std::endl; return 0; }
void PhysicsShape::setScale(float scaleX, float scaleY) { if (fabs(_scaleX - scaleX) > FLT_EPSILON || fabs(_scaleY - scaleY) > FLT_EPSILON) { if (_type == Type::CIRCLE && scaleX != scaleY) { CCLOG("PhysicsShapeCircle WARNING: CANNOT support setScale with different x and y"); return; } _newScaleX = scaleX; _newScaleY = scaleY; updateScale(); // re-calculate area and mass _area = calculateArea(); _mass = _material.density * _area; _moment = calculateDefaultMoment(); } }
bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Vec2& offset /*= Vec2(0, 0)*/) { do { _type = Type::CIRCLE; auto shape = cpCircleShapeNew(s_sharedBody, radius, PhysicsHelper::point2cpv(offset)); CC_BREAK_IF(shape == nullptr); cpShapeSetUserData(shape, this); addShape(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE)); cpShape* shape = cpCircleShapeNew(_info->getSharedBody(), radius, PhysicsHelper::point2cpv(offset)); CC_BREAK_IF(shape == nullptr); _info->add(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); setMaterial(material); return true; } while (false); return false; }
bool Triangulator::isConvex(const he::PrimitiveList<vec2>& vertices) { if (vertices.size() < 3) return false; if (vertices.size() == 3) return true; int winding(sign(calculateArea(vertices))); int a((int)vertices.size() - 4); int b((int)vertices.size() - 2); vec2 t1,t2,t3; he::PrimitiveList<vec2> tri; for (uint32 c(0); c < vertices.size(); ++c) { t1 = vertices[a]; t2 = vertices[b]; t3 = vertices[c]; tri.clear(); tri.add(t1); tri.add(t2); tri.add(t3); if (sign(calculateArea(tri)) != winding) return false; a = b; b = c; } return true; }
double element::getArea() { calculateArea(); return currentArea; }
//---------------------------------------- void ofxBox2dPolygon::updateShape() { calculateArea(); calculateCentroid(); bounds = getBoundingBox(); }
void element::splitBorderLocal(int longEdge, int opnode, int othernode, int modEdge) { // split a triangle with longest edge on border // newElem nodes are numbered as in parens below /* opnode(0) /@\ / | \ / | \ otherEdge / | \ modEdge / n| \ / e| \ / w| \ / E| \ / d| \ / g| \ / e| newElem \ / | \ @____________@____________@ othernode(1) longEdge m(2) newLong */ int fixnode = 3 - opnode - othernode; // find midpoint on longest edge node m; // the new node C->theNodes[nodes[othernode]].midpoint(C->theNodes[nodes[fixnode]], &m); // add new components to local chunk and get refs to them int mIdx = C->addNode(m); edgeRef newEdgeRef = C->addEdge(); edgeRef newLongRef = C->addEdge(); elemRef newElemRef; if (opnode == 0) { if (othernode == 1) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newLongRef, newEdgeRef); else // othernode == 2 newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newEdgeRef, newLongRef, edges[modEdge]); } else if (opnode == 1) { if (othernode == 0) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newEdgeRef, newLongRef); else // othernode == 2 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newEdgeRef, edges[modEdge], newLongRef); } else { // opnode is 2 if (othernode == 0) newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newLongRef, newEdgeRef, edges[modEdge]); else // othernode == 1 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newLongRef, edges[modEdge], newEdgeRef); } edges[modEdge].updateElement(C, myRef, newElemRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, myRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, newElemRef); C->theEdges[newLongRef.idx].updateElement(nullRef, newElemRef); nodes[othernode] = mIdx; edges[modEdge] = newEdgeRef; C->theElements[newElemRef.idx].setTargetArea(targetArea); // tell the world outside about the split if (C->theClient) C->theClient->split(myRef.idx, longEdge, othernode, 0.5, LOCAL_FIRST); tellDepend(); // tell dependent it can go now calculateArea(); // update cached area of original element C->theElements[newElemRef.idx].calculateArea(); // and of new element }
bool testShape2D ( SWManifold& manifold , const SWShape2D* shape1, const tmat33& mat1 , const SWShape2D* shape2, const tmat33& mat2 ) { tvec2 simplex[3]; //! find first simplex { tvec2 dir, farthest1, farthest2; dir = tvec2::axisX; shape1->getFarthest( farthest1, dir, mat1 ); shape2->getFarthest( farthest2, -dir, mat2 ); simplex[0] = farthest1 - farthest2; dir = (-simplex[0]).normal(); shape1->getFarthest( farthest1, dir, mat1 ); shape2->getFarthest( farthest2, -dir, mat2 ); simplex[1] = farthest1 - farthest2; tvec2 line = (simplex[1] - simplex[0]); if ( line.length() < SW_Epsilon ) return false; float kz = line.cross( simplex[0] ); if ( SWMath.abs(kz) < SW_Epsilon ) { if ( line.dot( -simplex[0] ) < 0 ) return false; if ( -line.dot( -simplex[1] ) < 0 ) return false; kz = 1; } dir = line.cross( kz ).normal(); shape1->getFarthest( farthest1, dir, mat1 ); shape2->getFarthest( farthest2, -dir, mat2 ); simplex[2] = farthest1 - farthest2; } //! try a few times to find overriding using Minkowski Difference and GJK tuint trying = 32; while ( trying-- ) { float simplesArea = calculateArea(simplex[0], simplex[1], simplex[2]); if ( simplesArea < SW_Epsilon ) return false; float originArea = 0; originArea += calculateArea( tvec2::zero, simplex[0], simplex[1]); originArea += calculateArea( tvec2::zero, simplex[1], simplex[2]); originArea += calculateArea( tvec2::zero, simplex[2], simplex[0]); //! is it close enough if ( SWMath.abs(originArea - simplesArea) < SW_Epsilon ) { //! finds normal and depth using EPA. //! we only try as much as maxTrying to find them const tuint maxTrying = 16; tvec2 polytope[maxTrying]; tvec2 center = tvec2::zero; tuint count = 0; //! first, fills polytope with simplex that contains the origin. center += polytope[count] = simplex[count];++count; center += polytope[count] = simplex[count];++count; center += polytope[count] = simplex[count];++count; center /= count; while ( count < maxTrying ) { tuint insertPos = 0; tvec2 direction; float closest = FLT_MAX; for ( tuint i = 0 ; i < count ; ++i ) { tvec2 edge = polytope[(i+1)%count] - polytope[i]; float kz = edge.cross( center-polytope[i] ); tvec2 dir = edge.cross(kz).normal(); float length = dir.dot(polytope[i]); if ( length < closest ) { insertPos = i+1; direction = dir; closest = length; } } //! finds new farthest point. tvec2 farthest1, farthest2; shape1->getFarthest( farthest1, direction, mat1 ); shape2->getFarthest( farthest2, -direction, mat2 ); tvec2 farthest = farthest1 - farthest2; //! save the result manifold.normal = direction; manifold.depth = closest; //! is it close enough float length = direction.dot(farthest); if ( SWMath.abs(length - closest) < SW_Epsilon ) { manifold.normal = manifold.normal.normal(); break; } else { tuint itor = count; while ( itor > insertPos ) { polytope[itor--] = polytope[itor]; } polytope[insertPos] = farthest; count += 1; } } //! finds incident points using edge clipping. { tuint count = 0; struct {tvec2 v1,v2;} ref, inc; shape2->getCrossest( inc.v1, inc.v2, -manifold.normal, mat2); if ( (inc.v1-inc.v2).length() < SW_Epsilon ) manifold.points[count++] = (inc.v1+inc.v2)*0.5f; if ( count == 0 ) { shape1->getCrossest( ref.v1, ref.v2, manifold.normal, mat1); clipToEdge( inc.v1, inc.v2, ref.v1, ref.v2 ); tvec2 edgeNormal = (ref.v2 - ref.v1).normal(); edgeNormal = -edgeNormal.cross( edgeNormal.cross(manifold.normal) ); float d1, d2; d1 = edgeNormal.dot( inc.v1 - ref.v1 ); d2 = edgeNormal.dot( inc.v2 - ref.v1 ); if ( d1 <= 0 ) {manifold.points[count++] = inc.v1;} if ( d2 <= 0 ) {manifold.points[count++] = inc.v2;} if ( count == 2 ) { if ( (inc.v1-inc.v2).length() < SW_Epsilon ) { manifold.points[0] = (inc.v1+inc.v2)*0.5f; count = 1; } } } manifold.count = count; } return true; } else { //! finds sides where origin is. tflag8 sideFlag; calculateSide( sideFlag, simplex[0], simplex[1], simplex[2] ); tuint count = 0; tuint index = 0; //! meaning of outside is opposite side of center of polygon in each edges. //! if there are two outsides, //! it means that the shapes don't meet each other in case of the convex-shape. if ( sideFlag.get(0) ) { index = 0; count += 1;} if ( sideFlag.get(1) ) { index = 1; count += 1;} if ( sideFlag.get(2) ) { index = 2; count += 1;} if ( count >= 2 ) return false; //! finds normal to the origin from a edge. tvec2 edge = simplex[(index+1)%3]-simplex[index]; float kz = edge.cross(simplex[index]); tvec2 dir = edge.cross( kz ).normal(); //! finds new farthest point. tvec2 farthest1, farthest2; shape1->getFarthest( farthest1, dir, mat1 ); shape2->getFarthest( farthest2, -dir, mat2 ); simplex[(index+2)%3] = farthest1 - farthest2; } } return false; }
void element::splitHelp(int longEdge) { int othernode, opnode, modEdge, otherEdge; int newNodeIdx; // neighbor has refined: newNode, otherNode and newLongEdgeRef sent and // stored locally by requestResponse // initializations of shortcuts to affected parts of element opnode = (longEdge + 2) % 3; othernode = longEdge; modEdge = opnode; otherEdge = (longEdge + 1) % 3; if (!(otherNode == C->theNodes[nodes[othernode]])) { othernode = (longEdge + 1) % 3; modEdge = othernode; otherEdge = opnode; } edgeRef modEdgeRef = edges[modEdge]; // lock perimeter if (!edges[modEdge].lock(C)) return; if (!edges[otherEdge].lock(C)) { edges[modEdge].unlock(C); return; } // add new components and make proper connections newNodeIdx = C->addNode(newNode); edgeRef newEdgeRef = C->addEdge(); elemRef newElemRef; if (opnode == 0) { if (othernode == 1) newElemRef = C->addElement(nodes[0], nodes[1], newNodeIdx, edges[modEdge], newLongEdgeRef, newEdgeRef); else // othernode == 2 newElemRef = C->addElement(nodes[0], newNodeIdx, nodes[2], newEdgeRef, newLongEdgeRef, edges[modEdge]); } else if (opnode == 1) { if (othernode == 0) newElemRef = C->addElement(nodes[0], nodes[1], newNodeIdx, edges[modEdge], newEdgeRef, newLongEdgeRef); else // othernode == 2 newElemRef = C->addElement(newNodeIdx, nodes[1], nodes[2], newEdgeRef, edges[modEdge], newLongEdgeRef); } else { // opnode is 2 if (othernode == 0) newElemRef = C->addElement(nodes[0], newNodeIdx, nodes[2], newLongEdgeRef, newEdgeRef, edges[modEdge]); else // othernode == 1 newElemRef = C->addElement(newNodeIdx, nodes[1], nodes[2], newLongEdgeRef, edges[modEdge], newEdgeRef); } C->theEdges[newEdgeRef.idx].updateElement(nullRef, myRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, newElemRef); newLongEdgeRef.updateElement(C, nullRef, newElemRef); edges[modEdge].updateElement(C, myRef, newElemRef); C->theElements[newElemRef.idx].setTargetArea(targetArea); nodes[othernode] = newNodeIdx; edges[modEdge] = newEdgeRef; // tell the world outside about the split if (C->theClient) C->theClient->split(myRef.idx, longEdge, othernode, 0.5, BOUND_SECOND); tellDepend(); // tell dependent it can go now specialRequest = pendingRequest = requestResponse = 0; // reset flags // unlock perimeter edges[longEdge].unlock(C); newLongEdgeRef.unlock(C); edges[otherEdge].unlock(C); modEdgeRef.unlock(C); // calculate areas of original and new element and cache results calculateArea(); C->theElements[newElemRef.idx].calculateArea(); }
int largestRectangleArea(vector<int> &height) { return calculateArea(height, 0, (int)height.size()-1); }
void element::splitNeighborsLocal(int longEdge, int opnode, int othernode, int modEdge, int nbrLongEdge, int nbrOpnode, int nbrOthernode, int nbrModEdge, const elemRef &nbr) { // find the new node along longEdge int fixnode = 3 - opnode - othernode; node m; C->theNodes[nodes[othernode]].midpoint(C->theNodes[nodes[fixnode]], &m); // add new components to local chunk and get refs to them int mIdx = C->addNode(m); edgeRef newEdgeRef = C->addEdge(); edgeRef newLongRef = C->addEdge(); edgeRef newNbrEdgeRef = C->addEdge(); elemRef newElemRef; if (opnode == 0) { if (othernode == 1) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newLongRef, newEdgeRef); else // othernode == 2 newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newEdgeRef, newLongRef, edges[modEdge]); } else if (opnode == 1) { if (othernode == 0) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newEdgeRef, newLongRef); else // othernode == 2 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newEdgeRef, edges[modEdge], newLongRef); } else { // opnode is 2 if (othernode == 0) newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newLongRef, newEdgeRef, edges[modEdge]); else // othernode == 1 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newLongRef, edges[modEdge], newEdgeRef); } elemRef newNbrRef; if (nbrOpnode == 0) { if (nbrOthernode == 1) newNbrRef = C->addElement(C->theElements[nbr.idx].nodes[0], C->theElements[nbr.idx].nodes[1], mIdx, C->theElements[nbr.idx].edges[nbrModEdge], newLongRef, newNbrEdgeRef); else // nbrOthernode == 2 newNbrRef = C->addElement(C->theElements[nbr.idx].nodes[0], mIdx, C->theElements[nbr.idx].nodes[2], newNbrEdgeRef, newLongRef, C->theElements[nbr.idx].edges[nbrModEdge]); } else if (nbrOpnode == 1) { if (nbrOthernode == 0) newNbrRef = C->addElement(C->theElements[nbr.idx].nodes[0], C->theElements[nbr.idx].nodes[1], mIdx, C->theElements[nbr.idx].edges[nbrModEdge], newNbrEdgeRef, newLongRef); else // nbrOthernode == 2 newNbrRef = C->addElement(mIdx, C->theElements[nbr.idx].nodes[1], C->theElements[nbr.idx].nodes[2], newNbrEdgeRef, C->theElements[nbr.idx].edges[nbrModEdge], newLongRef); } else { // nbrOpnode is 2 if (nbrOthernode == 0) newNbrRef = C->addElement(C->theElements[nbr.idx].nodes[0], mIdx, C->theElements[nbr.idx].nodes[2], newLongRef, newNbrEdgeRef, C->theElements[nbr.idx].edges[nbrModEdge]); else // nbrOthernode == 1 newNbrRef = C->addElement(mIdx, C->theElements[nbr.idx].nodes[1], C->theElements[nbr.idx].nodes[2], newLongRef, C->theElements[nbr.idx].edges[nbrModEdge], newNbrEdgeRef); } // link everything together properly edges[modEdge].updateElement(C, myRef, newElemRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, myRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, newElemRef); C->theEdges[newLongRef.idx].updateElement(nullRef, newElemRef); C->theEdges[newLongRef.idx].updateElement(nullRef, newNbrRef); C->theEdges[newNbrEdgeRef.idx].updateElement(nullRef, nbr); C->theEdges[newNbrEdgeRef.idx].updateElement(nullRef, newNbrRef); C->theElements[nbr.idx].edges[nbrModEdge].updateElement(C, nbr, newNbrRef); C->theElements[newElemRef.idx].setTargetArea(targetArea); C->theElements[newNbrRef.idx].setTargetArea(C->theElements[nbr.idx].getTargetArea()); nodes[othernode] = mIdx; edges[modEdge] = newEdgeRef; C->theElements[nbr.idx].nodes[nbrOthernode] = mIdx; C->theElements[nbr.idx].edges[nbrModEdge] = newNbrEdgeRef; // tell the world outside about the split if (C->theClient) C->theClient->split(myRef.idx, longEdge, othernode, 0.5, LOCAL_FIRST); if (C->theClient) C->theClient->split(nbr.idx, nbrLongEdge, nbrOthernode, 0.5, LOCAL_SECOND); // tell dependents they can go now tellDepend(); C->theElements[nbr.idx].tellDepend(); // calculate new areas for the original two elements and the two new // ones and cache the results calculateArea(); C->theElements[newElemRef.idx].calculateArea(); C->theElements[newNbrRef.idx].calculateArea(); C->theElements[nbr.idx].calculateArea(); }
//---------------------------------------- void ofxBox2dPolygon::updateShape() { calculateArea(); calculateCentroid(); }
void element::splitResponse(int longEdge) { int opnode, othernode, modEdge, otherEdge; // this element is first to refine of a pair of elements on differing chunks // initializations of shortcuts to affected parts of element opnode = (longEdge + 2) % 3; othernode = longEdge; modEdge = opnode; otherEdge = (longEdge + 1) % 3; edgeRef modEdgeRef = edges[modEdge]; // lock perimeter if (!edges[longEdge].lock(C)) return; if (!edges[modEdge].lock(C)) { edges[longEdge].unlock(C); return; } if (!edges[otherEdge].lock(C)) { edges[modEdge].unlock(C); edges[longEdge].unlock(C); return; } // find midpoint on longest edge int fixnode = 3 - opnode - othernode; node m; C->theNodes[nodes[othernode]].midpoint(C->theNodes[nodes[fixnode]], &m); // add new components to local chunk and get refs to them int mIdx = C->addNode(m); edgeRef newEdgeRef = C->addEdge(); edgeRef newLongRef = C->addEdge(); elemRef newElemRef; if (opnode == 0) { if (othernode == 1) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newLongRef, newEdgeRef); else // othernode == 2 newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newEdgeRef, newLongRef, edges[modEdge]); } else if (opnode == 1) { if (othernode == 0) newElemRef = C->addElement(nodes[0], nodes[1], mIdx, edges[modEdge], newEdgeRef, newLongRef); else // othernode == 2 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newEdgeRef, edges[modEdge], newLongRef); } else { // opnode is 2 if (othernode == 0) newElemRef = C->addElement(nodes[0], mIdx, nodes[2], newLongRef, newEdgeRef, edges[modEdge]); else // othernode == 1 newElemRef = C->addElement(mIdx, nodes[1], nodes[2], newLongRef, edges[modEdge], newEdgeRef); } edges[modEdge].updateElement(C, myRef, newElemRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, myRef); C->theEdges[newEdgeRef.idx].updateElement(nullRef, newElemRef); C->theEdges[newLongRef.idx].updateElement(nullRef, newElemRef); if (!newLongRef.lock(C)) CkAbort("ERROR locking new long edge.\n"); // tell other half of pair that it can proceed with refinement mesh[specialRequester.cid].specialRequestResponse(specialRequester.idx, m.X(), m.Y(), C->theNodes[nodes[othernode]].X(), C->theNodes[nodes[othernode]].Y(), newLongRef); nodes[othernode] = mIdx; edges[modEdge] = newEdgeRef; C->theElements[newElemRef.idx].setTargetArea(targetArea); // tell the world outside about the split if (C->theClient) C->theClient->split(myRef.idx, longEdge, othernode, 0.5, BOUND_FIRST); specialRequest = pendingRequest = requestResponse = 0; // reset flags tellDepend(); // tell dependent it can go now // unlock perimeter edges[otherEdge].unlock(C); modEdgeRef.unlock(C); // calculate aeras of original and new elements and cache results calculateArea(); C->theElements[newElemRef.idx].calculateArea(); }
void mapRoverTask( void *param ){ //static portTASK_FUNCTION( mapRoverTask, pvParameters ) RoverMapStruct *roverMapStruct = (RoverMapStruct *) param; MapCorner receivedCorner; double totalAngle = 0; double totalCalcAngle = 90.0; double area; char buf[20]; double number; //int intPart, decimalPart; uint8_t cornersCount = 0; sprintf(guiMapCoordinates, ""); polyComp = 0; for(;;){ if (xQueueReceive(roverMapStruct->inQ, (void *) &receivedCorner, portMAX_DELAY) != pdTRUE) { VT_HANDLE_FATAL_ERROR(0); } if(strlen(guiMapCoordinates) >= (BUFFER_SIZE - 10)){ sprintf(guiMapCoordinates, ""); } if(strlen(debugBuf) >= (BUFFER_SIZE - 10)){ sprintf(debugBuf, ""); } printFloat("QReced:", receivedCorner.distSide, 1); // Print the received distance reading vtLEDToggle(0x40); printFloat("Angle: ", receivedCorner.angleCornerExterior, 0);; double ang; if(roverMapStruct->taskFlags == REGULAR && roverMapStruct->numberSides != 0){ receivedCorner.angleCornerExterior = 360.0/roverMapStruct->numberSides; } else{ ang = receivedCorner.angleCornerExterior; receivedCorner.angleCornerExterior = receivedCorner.angleCornerExterior + receivedCorner.tempBefore/2.0; if(receivedCorner.angleCornerExterior>90) receivedCorner.angleCornerExterior = 90; } //printFloat("Side Dist: ", receivedCorner.distSide, 1); receivedCorner.tempPow = mapCorners[cornersCount-1].distFromSide * pow(receivedCorner.angleCornerExterior/90.0, 5); totalAngle += receivedCorner.angleCornerExterior; if(cornersCount != 0){ //totalAngle += receivedCorner.angleCornerExterior; //receivedCorner.distSide += mapCorners[cornersCount-1].distFromSide; printFloat("PrevSide:", mapCorners[cornersCount-1].distFromSide, 1); printFloat("WithPrevSide:", receivedCorner.distSide, 1); // Teja experimenting receivedCorner.distSide += receivedCorner.tempFrontDist + mapCorners[cornersCount-1].distFromSide*sin(mapCorners[cornersCount-1].angleCornerExterior*M_PI/180.0)*sin(mapCorners[cornersCount-1].angleCornerExterior*M_PI/180.0); // Yasir's conversion to coordinates xPoints[cornersCount] = xPoints[cornersCount - 1] + receivedCorner.distSide*cos(totalCalcAngle*M_PI/180.0); yPoints[cornersCount] = yPoints[cornersCount - 1] + receivedCorner.distSide*sin(totalCalcAngle*M_PI/180.0); totalCalcAngle -= receivedCorner.angleCornerExterior; sprintf(buf, "C=%d Deg=%.2f Dist=%.2f A=%.2f\n", cornersCount+1, receivedCorner.angleCornerExterior, receivedCorner.distSide, calculateArea(cornersCount+1, xPoints, yPoints)); strcat(debugBuf, buf); //sprintf(buf, "Deg=%.1f F=%.1f OA=%.1f SB=%.1f SD=%.1f SDP=%1.f FD=%.1f Dist=%.1f S=%d\n", receivedCorner.angleCornerExterior,receivedCorner.tempFront, ang, receivedCorner.tempBefore, mapCorners[cornersCount-1].distFromSide, receivedCorner.tempPow, receivedCorner.tempFrontDist, receivedCorner.distSide, cornersCount); //strcat(debugBuf, buf); //sprintf(buf, "A=%f\n", calculateArea(cornersCount+1, xPoints, yPoints)); //strcat(debugBuf, buf); if ((totalAngle) >= TOTAL_ANGLE_THRESHOLD){ // TODO: calculate area; // param for calculateArea (side) is 1 minus the number of sides if(polyComp == 0){ polyComp = -1; sprintf(buf, "%d,%d ", (int)(xPoints[cornersCount]*MAP_SCALE_FACTOR + MAP_X_OFFSET), (int)(yPoints[cornersCount]*-MAP_SCALE_FACTOR + MAP_Y_OFFSET)); strcat(guiMapCoordinates, buf); } else{ polyComp = cornersCount; sprintf(buf, "%d,%d", MAP_X_OFFSET, MAP_Y_OFFSET); strcat(guiMapCoordinates, buf); } sprintf(buf, "\n** Polygon complete ** Corners=%d A=%f\n\n", cornersCount+1,calculateArea(cornersCount+1, xPoints, yPoints)); strcat(debugBuf, buf); } else{ sprintf(buf, "%d,%d ", (int)(xPoints[cornersCount]*MAP_SCALE_FACTOR + MAP_X_OFFSET), (int)(yPoints[cornersCount]*-MAP_SCALE_FACTOR + MAP_Y_OFFSET)); strcat(guiMapCoordinates, buf); } } else{ totalCalcAngle = 90; xPoints[0] = 0; xPoints[0] = 0; sprintf(buf, "%d,%d ", (int)(xPoints[cornersCount]*MAP_SCALE_FACTOR + MAP_X_OFFSET), (int)(yPoints[cornersCount]*-MAP_SCALE_FACTOR + MAP_Y_OFFSET)); strcat(guiMapCoordinates, buf); } if(cornersCount < MAXIMUM_CORNERS){ mapCorners[cornersCount] = receivedCorner; ++cornersCount; } } }