void FallBackSplitter<Heuristic,PrimRefBlockList>::split(size_t threadIndex, PrimRefAlloc* alloc, const RTCGeometry* geom, PrimRefBlockList& prims, const PrimInfo& pinfo, PrimRefBlockList& lprims, PrimInfo& linfo, Split& lsplit, PrimRefBlockList& rprims, PrimInfo& rinfo, Split& rsplit) { /* enforce split */ size_t lnum = 0; BBox3f lgeomBounds = empty; BBox3f lcentBounds = empty; size_t rnum = 0; BBox3f rgeomBounds = empty; BBox3f rcentBounds = empty; atomic_set<PrimRefBlock>::item* lblock = lprims.insert(alloc->malloc(threadIndex)); atomic_set<PrimRefBlock>::item* rblock = rprims.insert(alloc->malloc(threadIndex)); while (atomic_set<PrimRefBlock>::item* block = prims.take()) { for (size_t i=0; i<block->size(); i++) { const PrimRef& prim = block->at(i); const BBox3f bounds = prim.bounds(); if ((lnum+rnum)&1) { lnum++; lgeomBounds.grow(bounds); lcentBounds.grow(center2(bounds)); if (likely(lblock->insert(prim))) continue; lblock = lprims.insert(alloc->malloc(threadIndex)); lblock->insert(prim); } else { rnum++; rgeomBounds.grow(bounds); rcentBounds.grow(center2(bounds)); if (likely(rblock->insert(prim))) continue; rblock = rprims.insert(alloc->malloc(threadIndex)); rblock->insert(prim); } } } new (&linfo) PrimInfo(lnum,lgeomBounds,lcentBounds); new (&rinfo) PrimInfo(rnum,rgeomBounds,rcentBounds); /* perform binning of left side */ Heuristic lheuristic(linfo,geom); typename PrimRefBlockList::iterator liter(lprims); while (typename PrimRefBlockList::item* block = liter.next()) { lheuristic.bin(block->base(),block->size()); } lheuristic.best(lsplit); /* perform binning of right side */ Heuristic rheuristic(rinfo,geom); typename PrimRefBlockList::iterator riter(rprims); while (typename PrimRefBlockList::item* block = riter.next()) { rheuristic.bin(block->base(),block->size()); } rheuristic.best(rsplit); }
void DrawMatches( const std::string& title, const cv::Mat image, const cv::Mat points1, const cv::Mat points2, const cv::Scalar& color1, const cv::Scalar& color2, bool draw_image_borders) { cv::Mat draw_image; if (image.type() == CV_8U) { cvtColor(image, draw_image, CV_GRAY2BGR); } else { draw_image = image.clone(); } for (int i = 0; i < points1.rows; i++) { cv::Point2f center1( cvRound(points1.at<cv::Vec2f>(0, i)[0] * 16.0), cvRound(points1.at<cv::Vec2f>(0, i)[1] * 16.0)); cv::Point2f center2(cvRound( points2.at<cv::Vec2f>(0, i)[0] * 16.0), cvRound(points2.at<cv::Vec2f>(0, i)[1] * 16.0)); circle(draw_image, center1, 48, color1, 1, CV_AA, 4); line(draw_image, center1, center2, color2, 1, CV_AA, 4); } opencv_util::ShowScaled(title, draw_image); }
void BaseVH::buildMostOrthogonalCameras() { int numSilhouetteCams = mSilhouetteCameras.size(); int numCams = mCalibration->get_cam_count(); mMostOrthogonalCamera.clear(); mMostOrthogonalCamera.resize( numCams , -1 ); mIsCameraUsed.clear(); mIsCameraUsed.resize( numCams , 0 ); for( int cc = 0; cc < numCams; cc++ ) { int camId1 = cc; Vector3d cameraRay1; double width1 = mCalibration->get_image_width( camId1 ); double height1 = mCalibration->get_image_height( camId1 ); Eigen::Vector2d center1( width1 / 2 , height1 / 2 ); mCalibration->getRay( camId1 , center1 , cameraRay1 ); double prevVal = 1; int mostOrthogonalCam = -1; for( int sc = 0; sc < numSilhouetteCams; sc++ ) { int camId2 = mSilhouetteCameras[ sc ]; double width2 = mCalibration->get_image_width( camId2 ); double height2 = mCalibration->get_image_height( camId2 ); Eigen::Vector2d center2( width2 / 2 , height2 / 2 ); Vector3d cameraRay2; mCalibration->getRay( camId2 , center2 , cameraRay2 ); double dot = cameraRay1.dot( cameraRay2 ); if( abs( dot ) < prevVal ) { prevVal = abs( dot ); mostOrthogonalCam = camId2; } } mMostOrthogonalCamera[ camId1 ] = mostOrthogonalCam; } }
PrimRefGen<Heuristic>::PrimRefGen(size_t threadIndex, size_t threadCount, const BuildSource* geom, PrimRefAlloc* alloc) : geom(geom), numPrimitives(0), numVertices(0), alloc(alloc) { /* compute number of primitives */ size_t numGroups = geom->groups(); for (size_t g=0; g<numGroups; g++) { size_t vertices = 0; numPrimitives += geom->prims(g,&vertices); numVertices += vertices; } /* approximate bounds */ BBox3fa geomBound = empty, centBound = empty; size_t s = 0, t = 0, dt = max(size_t(1),numPrimitives/2048); for (size_t g=0; g<numGroups; g++) { size_t numPrims = geom->prims(g); for (size_t i=t-s; i<numPrims; i+=dt, t+=dt) { BBox3fa bounds = geom->bounds(g,i); geomBound.extend(bounds); centBound.extend(center2(bounds)); } s += numPrims; } new (&pinfo) PrimInfo(numPrimitives,geomBound,centBound); /* compute start group and primitives */ size_t g=0, i=0; for (size_t k=0; k<numTasks; k++) { size_t start = (k+0)*numPrimitives/numTasks; size_t end = (k+1)*numPrimitives/numTasks; size_t size = end-start; work[k].startGroup = g; work[k].startPrim = i; work[k].numPrims = size; for (; g<numGroups; g++) { size_t numPrims = geom->prims(g)-i; if (size < numPrims) { i += size; break; } size -= numPrims; i = 0; } } /* start parallel task */ TaskScheduler::executeTask(threadIndex,threadCount, _task_gen_parallel,this,numTasks, _task_gen_parallel_reduce,this, "build::primrefgen"); }
void FallBackSplitter<Heuristic,PrimRefBlockList>::split(size_t threadIndex, PrimRefAlloc* alloc, const RTCGeometry* geom, PrimRefBlockList& prims, const PrimInfo& pinfo, PrimRefBlockList& lprims, PrimInfo& linfo, PrimRefBlockList& rprims, PrimInfo& rinfo) { /* enforce split */ size_t lnum = 0; BBox3f lgeomBounds = empty; BBox3f lcentBounds = empty; size_t rnum = 0; BBox3f rgeomBounds = empty; BBox3f rcentBounds = empty; atomic_set<PrimRefBlock>::item* lblock = lprims.insert(alloc->malloc(threadIndex)); atomic_set<PrimRefBlock>::item* rblock = rprims.insert(alloc->malloc(threadIndex)); while (atomic_set<PrimRefBlock>::item* block = prims.take()) { for (size_t i=0; i<block->size(); i++) { const PrimRef& prim = block->at(i); const BBox3f bounds = prim.bounds(); if ((lnum+rnum)&1) { lnum++; lgeomBounds.grow(bounds); lcentBounds.grow(center2(bounds)); if (likely(lblock->insert(prim))) continue; lblock = lprims.insert(alloc->malloc(threadIndex)); lblock->insert(prim); } else { rnum++; rgeomBounds.grow(bounds); rcentBounds.grow(center2(bounds)); if (likely(rblock->insert(prim))) continue; rblock = rprims.insert(alloc->malloc(threadIndex)); rblock->insert(prim); } } } new (&linfo) PrimInfo(lnum,lgeomBounds,lcentBounds); new (&rinfo) PrimInfo(rnum,rgeomBounds,rcentBounds); }
//This function sets up the two shapes we need for this example. void setup() { int NumberOfDivisions = 20; line.point1 = glm::vec3(-0.5f, 0.5f, 0.0f); line.point2 = glm::vec3(-0.5f, -0.5f, 0.0f); float radius = 0.25f; cylinder.radius = radius; cylinder.point1 = glm::vec3(0.0f, 0.5f, 0.0f); cylinder.point2 = glm::vec3(0.0f, -0.5f, 0.0f); VertexFormat center1(cylinder.point1, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)); VertexFormat center2(cylinder.point2, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)); std::vector<VertexFormat> vertices; float height = glm::distance(cylinder.point1, cylinder.point2); float theta = 360.0f / NumberOfDivisions; VertexFormat A, B, C, D; //Circle vertex generation //In this example we are not implementing the proper the code for indices. We are just going to produce redundant information in the buffer. //since we are only having a small number of objects on the screen currently, this redundancy should not matter. for (int i = 0; i < NumberOfDivisions; i++) { A = VertexFormat(glm::vec3(radius * cos(glm::radians(i*theta)),0.5f, radius * sin(glm::radians(i*theta))), glm::vec4(0.7f, 0.20f, 0.0f, 1.0f)); B = VertexFormat(glm::vec3(radius * cos(glm::radians((i + 1)*theta)), 0.5f, radius * sin(glm::radians((i + 1)*theta))), glm::vec4(0.7f, 0.20f, 0.0f, 1.0f)); C = VertexFormat(glm::vec3(radius * cos(glm::radians(i*theta)), -0.5f, radius * sin(glm::radians(i*theta))), glm::vec4(0.0f, 0.20f, 0.7f, 1.0f)); D = VertexFormat(glm::vec3(radius * cos(glm::radians((i + 1)*theta)), -0.5f, radius * sin(glm::radians((i + 1)*theta))), glm::vec4(0.0f, 0.20f, 0.7f, 1.0f)); //In every iteration, the center, the point at angle theta and at angle (theta+delta) are fed into the buffer. vertices.push_back(center1); vertices.push_back(A); vertices.push_back(B); vertices.push_back(center2); vertices.push_back(C); vertices.push_back(D); vertices.push_back(A); vertices.push_back(C); vertices.push_back(B); vertices.push_back(C); vertices.push_back(D); vertices.push_back(B); } cylinder.base.initBuffer(12 * NumberOfDivisions, &vertices[0]); }
int BaseVH::getMostOrthogonalUnusedCamera(int camId) { Vector3d cameraRay1; double width1 = mCalibration->get_image_width( camId ); double height1 = mCalibration->get_image_height( camId ); cv::Point2f center1( width1 / 2 , height1 / 2 ); mCalibration->getRay( camId , center1 , cameraRay1 ); double prevVal = 1; int mostOrthogonalCam = -1; int numSilhouetteCams = mSilhouetteCameras.size(); for( int sc = 0; sc < numSilhouetteCams; sc++ ) { int camId2 = mSilhouetteCameras[ sc ]; if( camId2 == camId ) continue; double width2 = mCalibration->get_image_width( camId2 ); double height2 = mCalibration->get_image_height( camId2 ); cv::Point2f center2( width2 / 2 , height2 / 2 ); Vector3d cameraRay2; mCalibration->getRay( camId2 , center2 , cameraRay2 ); double dot = cameraRay1.dot( cameraRay2 ); if( abs( dot ) < prevVal && !mIsCameraUsed[ camId2 ] ) { prevVal = dot; mostOrthogonalCam = camId2; } } return mostOrthogonalCam; }
void PrimRefGen<Heuristic>::task_gen_parallel(size_t threadIndex, size_t threadCount, size_t taskIndex, size_t taskCount, TaskScheduler::Event* event) { Heuristic& heuristic = heuristics[taskIndex]; new (&heuristic) Heuristic(pinfo,geom); /* static work allocation */ size_t g = work[taskIndex].startGroup; size_t i = work[taskIndex].startPrim; size_t numPrims = work[taskIndex].numPrims; size_t numGroupPrims = numPrims ? geom->prims(g) : 0; size_t numAddedPrims = 0; BBox3fa geomBound = empty, centBound = empty; typename atomic_set<PrimRefBlock>::item* block = prims.insert(alloc->malloc(threadIndex)); for (size_t p=0; p<numPrims; p++, i++) { /* goto next group */ while (i == numGroupPrims) { g++; i = 0; numGroupPrims = geom->prims(g); } const BBox3fa b = geom->bounds(g,i); if (b.empty()) continue; numAddedPrims++; geomBound.extend(b); centBound.extend(center2(b)); const PrimRef prim = PrimRef(b,g,i); if (likely(block->insert(prim))) continue; heuristic.bin(block->base(),block->size()); block = prims.insert(alloc->malloc(threadIndex)); block->insert(prim); } heuristic.bin(block->base(),block->size()); geomBounds[taskIndex] = geomBound; centBounds[taskIndex] = centBound; work[taskIndex].numPrims = numAddedPrims; }
void Glut2DView::Reshape(int w, int h) { // find screen centers before and after resize Vertex2d center(m_windowSize.x / 2.0, m_windowSize.y / 2.0); Vertex2d center2(w / 2.0, h / 2.0); Vertex2d wcenter = ScreenToWorld((int) center.x, (int) center.y); // move the focus to center of screen SetFocus(wcenter.x, wcenter.y); // find scaling factor for resizing double len1 = std::min(center.x, center.y); double len2 = std::min(center2.x, center2.y); double scale = len2 / len1; // change zoom and translation in order to keep world centered and in view m_trans = m_trans + center2 - center; m_zoom = m_zoom * scale; // set view port and window size glViewport(0, 0, w, h); m_windowSize.Set(w, h); // set projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, w, 0, h, -1.0, 1.0); // window has resized for (ListenerIter iter = m_listeners.begin(); iter != m_listeners.end(); iter++) { (*iter)->ViewResize(this); } MakeCurrentWindow(); }
void TectonicPlateSimulation::Cut(b2Vec2 from, b2Vec2 to) { bool pointsAreEqual = from.x == to.x && from.y == to.y; if(pointsAreEqual) return; std::vector<b2Body*> affectedBodies; std::vector<b2PolygonShape*> affectedPolygonShapes; std::vector<b2Vec2> points; MyRayCastCallback callback1; world.RayCast(&callback1, from, to); for(int i = 0; i < callback1.m_count; i++) { points.push_back(callback1.m_points[i]); //m_debugDraw.DrawPoint(points.back(), 5.0f, b2Color(0.9f, 0.4f, 0.4f)); if(std::find(affectedBodies.begin(), affectedBodies.end(), callback1.m_bodies[i]) == affectedBodies.end()) { // does not contain affectedBodies.push_back(callback1.m_bodies[i]); affectedPolygonShapes.push_back(callback1.m_polygonShapes[i]); } } MyRayCastCallback callback2; world.RayCast(&callback2, to, from); for(int i = 0; i < callback2.m_count; i++) { int index = (int)(find(affectedBodies.begin(), affectedBodies.end(), callback2.m_bodies[i]) - affectedBodies.begin()); if(index < affectedBodies.size()) { // does contain points.push_back(callback2.m_points[i]); //m_debugDraw.DrawPoint(points.back(), 5.0f, b2Color(0.4f, 0.4f, 0.9f)); b2Vec2 rayCenter((points.back().x + points[index].x) / 2.0f, (points.back().y + points[index].y) / 2.0f); //m_debugDraw.DrawPoint(rayCenter, 5.0f, b2Color(0.4f, 0.9f, 0.4f)); float rayAngle = atan2f(points[index].y - points.back().y, points[index].x - points.back().x); std::vector<b2Vec2> vertices1; std::vector<b2Vec2> vertices2; for(int j = 0; j < affectedPolygonShapes[index]->GetVertexCount(); j++) { b2Vec2 polygonVertex = affectedBodies[index]->GetWorldPoint(affectedPolygonShapes[index]->GetVertex(j)); float cutAngle = atan2f(polygonVertex.y - rayCenter.y, polygonVertex.x - rayCenter.x) - rayAngle; if(cutAngle < M_PI*-1.0f) { cutAngle += 2.0f * M_PI; } if(cutAngle > 0.0f && cutAngle <= M_PI) { vertices1.push_back(polygonVertex); //m_debugDraw.DrawPoint(polygonVertex, 5.0f, b2Color(0.4f, 0.9f, 0.9f)); } else { vertices2.push_back(polygonVertex); //m_debugDraw.DrawPoint(polygonVertex, 5.0f, b2Color(0.9f, 0.9f, 0.4f)); } } vertices1.push_back(points[index]); vertices1.push_back(points.back()); vertices2.push_back(points.back()); vertices2.push_back(points[index]); bool cutLegitimate = true; for(int j = 0; j < vertices1.size() && cutLegitimate; j++) { b2Vec2 edge = vertices1[(j + 1) % vertices1.size()] - vertices1[j]; if(edge.Length() <= 0.1f) cutLegitimate = false; } for(int j = 0; j < vertices2.size() && cutLegitimate; j++) { b2Vec2 edge = vertices2[(j + 1) % vertices2.size()] - vertices2[j]; if(edge.Length() <= 0.1f) cutLegitimate = false; } bool polygonDegenerate = false; float epsilon = 0.1f; for(int j = 0; j < vertices1.size() && !polygonDegenerate; j++) { b2Vec2 v1 = vertices1[j]; for(int k = 0; k < vertices1.size() && !polygonDegenerate; k++) { if(k == j) continue; b2Vec2 v2 = vertices1[k]; if(fabs(v1.x - v2.x) < epsilon && fabs(v1.y - v2.y) < epsilon) polygonDegenerate = true; } } for(int j = 0; j < vertices2.size() && !polygonDegenerate; j++) { b2Vec2 v1 = vertices2[j]; for(int k = 0; k < vertices2.size() && !polygonDegenerate; k++) { if(k == j) continue; b2Vec2 v2 = vertices2[k]; if(fabs(v1.x - v2.x) < epsilon && fabs(v1.y - v2.y) < epsilon) polygonDegenerate = true; } } if(vertices1.size() < 3 || vertices1.size() > 8 || vertices2.size() < 3 || vertices2.size() > 8 || polygonDegenerate) { if(i < callback2.m_count - 1) to = callback2.m_points[i+1]; else to = from; continue; } b2Vec2 center1(0,0); b2Vec2 center2(0,0); for(int i = 0; i < vertices1.size(); i++) { center1 += vertices1[i]; } center1 *= 1.0f / vertices1.size(); for(int i = 0; i < vertices1.size(); i++) { vertices1[i] -= center1; } for(int i = 0; i < vertices2.size(); i++) { center2 += vertices2[i]; } center2 *= 1.0f / vertices2.size(); for(int i = 0; i < vertices2.size(); i++) { vertices2[i] -= center2; } //body definition b2BodyDef myBodyDef; myBodyDef.type = b2_dynamicBody; //shape definition b2PolygonShape polygonShape; //fixture definition b2FixtureDef myFixtureDef; myFixtureDef.shape = &polygonShape; myFixtureDef.density = 1; myFixtureDef.restitution = 0.5; myBodyDef.position.Set(center1.x, center1.y); polygonShape.Set(&vertices1[0], (int)vertices1.size()); b2Body* body1 = world.CreateBody(&myBodyDef); body1->CreateFixture(&myFixtureDef); body1->SetLinearVelocity(affectedBodies[index]->GetLinearVelocity()); body1->SetAngularVelocity(affectedBodies[index]->GetAngularVelocity()); myBodyDef.position.Set(center2.x, center2.y); polygonShape.Set(&vertices2[0], (int)vertices2.size()); b2Body* body2 = world.CreateBody(&myBodyDef); body2->CreateFixture(&myFixtureDef); body2->SetLinearVelocity(affectedBodies[index]->GetLinearVelocity()); body2->SetAngularVelocity(affectedBodies[index]->GetAngularVelocity()); if(i < callback2.m_count - 1) to = callback2.m_points[i+1]; else to = from; world.DestroyBody(affectedBodies[index]); } } //m_debugDraw.DrawSegment(from, to, b2Color(0.8f, 0.8f, 0.8f)); }
void SpatialSplit::Split::split<false>(size_t threadIndex, size_t threadCount, LockStepTaskScheduler* scheduler, PrimRefBlockAlloc<PrimRef>& alloc, Scene* scene, TriRefList& prims, TriRefList& lprims_o, PrimInfo& linfo_o, TriRefList& rprims_o, PrimInfo& rinfo_o) const { assert(valid()); TriRefList::item* lblock = lprims_o.insert(alloc.malloc(threadIndex)); TriRefList::item* rblock = rprims_o.insert(alloc.malloc(threadIndex)); linfo_o.reset(); rinfo_o.reset(); /* sort each primitive to left, right, or left and right */ while (atomic_set<PrimRefBlock>::item* block = prims.take()) { for (size_t i=0; i<block->size(); i++) { const PrimRef& prim = block->at(i); const BBox3fa bounds = prim.bounds(); const int bin0 = mapping.bin(bounds.lower)[dim]; const int bin1 = mapping.bin(bounds.upper)[dim]; /* sort to the left side */ if (bin1 < pos) { linfo_o.add(bounds,center2(bounds)); if (likely(lblock->insert(prim))) continue; lblock = lprims_o.insert(alloc.malloc(threadIndex)); lblock->insert(prim); continue; } /* sort to the right side */ if (bin0 >= pos) { rinfo_o.add(bounds,center2(bounds)); if (likely(rblock->insert(prim))) continue; rblock = rprims_o.insert(alloc.malloc(threadIndex)); rblock->insert(prim); continue; } /* split and sort to left and right */ TriangleMesh* mesh = (TriangleMesh*) scene->get(prim.geomID()); TriangleMesh::Triangle tri = mesh->triangle(prim.primID()); const Vec3fa v0 = mesh->vertex(tri.v[0]); const Vec3fa v1 = mesh->vertex(tri.v[1]); const Vec3fa v2 = mesh->vertex(tri.v[2]); PrimRef left,right; float fpos = mapping.pos(pos,dim); splitTriangle(prim,dim,fpos,v0,v1,v2,left,right); if (!left.bounds().empty()) { linfo_o.add(left.bounds(),center2(left.bounds())); if (!lblock->insert(left)) { lblock = lprims_o.insert(alloc.malloc(threadIndex)); lblock->insert(left); } } if (!right.bounds().empty()) { rinfo_o.add(right.bounds(),center2(right.bounds())); if (!rblock->insert(right)) { rblock = rprims_o.insert(alloc.malloc(threadIndex)); rblock->insert(right); } } } alloc.free(threadIndex,block); } }
void DrawMatches( cv::Mat& image_out, const cv::Mat image1, const cv::Mat image2, const cv::Mat points1, const cv::Mat points2, const cv::Scalar& color, bool draw_image_borders, const cv::Scalar& point_color) { cv::Size size(image1.cols + image2.cols, std::max(image1.rows, image2.rows)); image_out.create(size, CV_MAKETYPE(image1.depth(), 3)); cv::Mat draw_image1 = image_out(cv::Rect(0, 0, image1.cols, image1.rows)); cv::Mat draw_image2 = image_out(cv::Rect(image1.cols, 0, image2.cols, image2.rows)); if (image1.type() == CV_8U) { cvtColor(image1, draw_image1, CV_GRAY2BGR); } else { image1.copyTo(draw_image1); } if (image2.type() == CV_8U) { cvtColor(image2, draw_image2, CV_GRAY2BGR); } else { image2.copyTo(draw_image2); } if (draw_image_borders) { cv::rectangle(draw_image1, cv::Point(0, 0), cv::Point(image1.cols, image1.rows), cv::Scalar(0, 0, 0), 2); cv::rectangle(draw_image2, cv::Point(0, 0), cv::Point(image2.cols, image2.rows), cv::Scalar(0, 0, 0), 2); } cv::RNG rng = cv::theRNG(); bool rand_color = color == cv::Scalar::all(-1); bool has_point_color = point_color != cv::Scalar::all(-1); for (int i = 0; i < points1.rows; i++) { cv::Scalar match_color = rand_color ? cv::Scalar(rng(256), rng(256), rng(256)) : color; cv::Scalar match_color2 = has_point_color ? point_color : match_color; cv::Point2f center1( cvRound(points1.at<cv::Vec2f>(0, i)[0] * 16.0), cvRound(points1.at<cv::Vec2f>(0, i)[1] * 16.0)); cv::Point2f center2( cvRound(points2.at<cv::Vec2f>(0, i)[0] * 16.0), cvRound(points2.at<cv::Vec2f>(0, i)[1] * 16.0)); cv::Point2f dcenter2( std::min(center2.x + draw_image1.cols * 16.0, (image_out.cols - 1) * 16.0), center2.y); circle(draw_image1, center1, 48, match_color2, 1, CV_AA, 4); circle(draw_image2, center2, 48, match_color2, 1, CV_AA, 4); line(image_out, center1, dcenter2, match_color, 1, CV_AA, 4); } }
// here we will redraw the scene according to the current state of the application. void AppWindow::glutDisplay () { // Clear the rendering window glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Build a cross with some lines (if not built yet): if ( _axis.changed ) // needs update { _axis.build(1.0f); // axis has radius 1.0 } if (sunanim) { sunx = 2 * (cos(2 * M_PI*sunxc / 360) + sin(2 * M_PI*sunxc / 360)); suny = 20.0f; sunz = 2 * (-sin(2 * M_PI*sunxz / 360) + cos(2 * M_PI*sunxz / 360)); } else { sunx = 0.0; suny = 1; sunz = -.5; } // Define our scene transformation: GsMat rx, ry, stransf, barrelroll, leftright, transf, updown, rightwing, leftwing, offsety, centerrwing, centerlwing, rl, rr, backR, backL, centerbackl, centerbackr, br, bl; GsMat rfrot, lfrot, rbrot, lbrot, rollyawpitch, ShadowT; rx.rotx ( _rotx ); ry.roty ( _roty ); stransf = rx*ry; // set the scene transformation matrix offsety.translation(GsVec(0.0f, -5.7f, 0.0f)); //Rotate many degrees barrelroll.rotz(2 * M_PI * rotate / 360); leftright.roty(2 * M_PI * _turnlr / 360); updown.rotx(2 * M_PI * _turnud / 360); rollyawpitch = leftright*updown*barrelroll; //Translate front wings to center centerrwing.translation(GsVec(-0.1f,-0.15f,0.0f)); centerlwing.translation(GsVec(0.1f, -0.15f, 0.0f)); //Translate front wings back to airplane rr.translation(GsVec(0.1f, 0.15f, 0.0f)); rl.translation(GsVec(-0.1f, 0.15f, 0.0f)); //Translate back wings to center centerbackl.translation(GsVec(-0.05f, -0.2f, 0.0f)); centerbackr.translation(GsVec(0.05f, -0.2f, 0.0f)); //Translate back wings to airplane bl.translation(GsVec(0.05f, 0.2f, 0.0f)); br.translation(GsVec(-0.05f, 0.2f, 0.0f)); //Rotate front wings rightwing.rotz(2 * M_PI * _wingsflyR / 360); leftwing.rotz(2 * M_PI * -_wingsflyL / 360); //Rotate back wings backR.rotz(2 * M_PI * _backR / 360); backL.rotz(2 * M_PI * -_backL / 360); //Clean up draw function rfrot = rr*rightwing*centerrwing; lfrot = rl*leftwing*centerlwing; rbrot = br*backR*centerbackr; lbrot = bl*backL*centerbackl; //speed is fast GsVec P = GsVec(0.0f, 0.0f, speed); GsVec bd = leftright*updown*barrelroll*P; R = R + bd; transf.setrans(R); GsVec sbd = leftright*P; SR = SR + sbd; ShadowT.setrans(SR); // Define our projection transformation: // (see demo program in gltutors-projection.7z, we are replicating the same behavior here) GsMat camview, camview2, _birdseye, persp, sproj; GsVec eye(0,0,0), center(0,0,0), up(0,1,0); GsVec eye2(0, 10, 0), center2(0, 0, 0), up2(0, 0, 1); eye += R + leftright*updown*barrelroll*GsVec(0,0,2); center += R + GsVec(0, 0, 0); _sun.build(1.0f, sunx, suny, sunz); float ground[4] = { 0, 1, 0, 4.99 }; float light[4] = { sunx, suny, sunz, 0 }; float dot; GsMat shadowMat; dot = ground[0] * light[0] + ground[1] * light[1] + ground[2] * light[2] + ground[3] * light[3]; shadowMat.setl1(dot - light[0] * ground[0], 0.0 - light[0] * ground[1], 0.0 - light[0] * ground[2], 0.0 - light[0] * ground[3]); shadowMat.setl2(0.0 - light[1] * ground[0], dot - light[1] * ground[1], 0.0 - light[1] * ground[2], 0.0 - light[1] * ground[3]); shadowMat.setl3(0.0 - light[2] * ground[0], 0.0 - light[2] * ground[1], dot - light[2] * ground[2], 0.0 - light[2] * ground[3]); shadowMat.setl4(0.0 - light[3] * ground[0], 0.0 - light[3] * ground[1], 0.0 - light[3] * ground[2], dot - light[3] * ground[3]); //shadowMat = shadowMat*ry*rx; camview.lookat ( eye, center, up ); // set our 4x4 "camera" matrix camview2.lookat(eye2, center2, up2); float aspect=1.0f, znear=0.1f, zfar=5000.0f; persp.perspective ( _fovy, aspect, znear, zfar ); // set our 4x4 perspective matrix // Our matrices are in "line-major" format, so vertices should be multiplied on the // right side of a matrix multiplication, therefore in the expression below camview will // affect the vertex before persp, because v' = (persp*camview)*v = (persp)*(camview*v). if (camera) { sproj = persp * camview; // set final scene projection } else { sproj = persp * camview2; } // Note however that when the shader receives a matrix it will store it in column-major // format, what will cause our values to be transposed, and we will then have in our // shaders vectors on the left side of a multiplication to a matrix. float col = 1; // Draw: //if ( _viewaxis ) _axis.draw ( stransf, sproj ); _model.draw(stransf*transf*rollyawpitch, sproj, _light, 0); _model2.draw(stransf*transf*rollyawpitch*rfrot, sproj, _light, 0); _model3.draw(stransf*transf*rollyawpitch*lfrot, sproj, _light, 0); _model4.draw(stransf*transf*rollyawpitch, sproj, _light, 0); _model5.draw(stransf*transf*rollyawpitch*rbrot, sproj, _light, 0); _model6.draw(stransf*transf*rollyawpitch*lbrot, sproj, _light, 0); _floor.draw(stransf, sproj, _light, textures); _city.draw(stransf*offsety, sproj, _light, 0); _city.draw(stransf*shadowMat*offsety, sproj, _shadow, 0); //Shadow _model.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _model2.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _model3.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _model4.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _model5.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _model6.draw(stransf*ShadowT*shadowMat*rollyawpitch, sproj, _shadow, 1); _side.draw(stransf, sproj, _light, col, textures); _sun.draw(stransf, sproj); // Swap buffers and draw: glFlush(); // flush the pipeline (usually not necessary) glutSwapBuffers(); // we were drawing to the back buffer, now bring it to the front }
void Colisionable::FixColision(sf::Vector2f pos1, sf::Vector2f size1, sf::Vector2f pos2, sf::Vector2f size2){ Map* map = Scene::getScene()->getMap(); //std::cout << "1x " << pos1.x << " 1y "<< pos1.y << " 2x" << pos2.x << " 2y " << pos2.y << std::endl; sf::Vector2f center1((pos1.x*2 + size1.x)/2, (pos1.y*2 + size1.y)/2); sf::Vector2f center2((pos2.x*2 + size2.x)/2, (pos2.y*2 + size2.y)/2); //std::cout << "cx " << center1.x << " cy "<< center1.y << " c2x" << center2.x << " c2y " << center2.y << std::endl; float top1 = pos1.y; float bottom1 = pos1.y+size1.y; float left1 = pos1.x; float right1 = pos1.x+ size1.x; float top2 = pos2.y; float bottom2 = pos2.y+size2.y; float left2 = pos2.x; float right2 = pos2.x+ size2.x; //primer quadrant if(center1.x > center2.x && center1.y <= center2.y){ //std::cout << "primer quadrant " << std::endl; float dist_left = right2-left1; float dist_bottom = bottom1 - top2; //std::cout << right2 << " "<< left1 << " " << dist_bottom << " " << dist_left << std::endl; //std::cout << "top " << size2.x << " bottom "<< bottom2 << " left" << left2 << " right " << right2 << std::endl; if(dist_left <= dist_bottom){ //Chunk* c = map->getChunk(center2.x + Settings::TILE_SIZE, center2.y); Tile* t = map->getTile(center2.x + Settings::TILE_SIZE, center2.y, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x, center2.y - Settings::TILE_SIZE); Tile* t2 = map->getTile(center2.x, center2.y - Settings::TILE_SIZE, 1); if(t2 == nullptr || t2->id !="0"){ if(col_left_dist < dist_left) col_left_dist = dist_left; if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; ++col_left; } else{ if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; } }else{ if(col_left_dist < dist_left) col_left_dist = dist_left; ++col_left; } } else { //Chunk* c = map->getChunk(center2.x, center2.y - Settings::TILE_SIZE); Tile* t = map->getTile(center2.x, center2.y - Settings::TILE_SIZE, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x + Settings::TILE_SIZE, center2.y); Tile* t2 = map->getTile(center2.x + Settings::TILE_SIZE, center2.y, 1); if(t2 == nullptr || t2->id !="0"){ if(col_left_dist < dist_left) col_left_dist = dist_left; if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_left; ++col_bottom; } else{ if(col_left_dist < dist_left) col_left_dist = dist_left; ++col_left; } } else{ if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; } } } //segon quadrant else if(center1.x <= center2.x && center1.y < center2.y){ //std::cout << "segon quadrant " << std::endl; float dist_right = right1 - left2; float dist_bottom = bottom1 - top2; if(dist_right <= dist_bottom){ //Chunk* c = map->getChunk(center2.x - Settings::TILE_SIZE, center2.y); Tile* t = map->getTile(center2.x - Settings::TILE_SIZE, center2.y, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x, center2.y - Settings::TILE_SIZE); Tile* t2 = map->getTile(center2.x, center2.y - Settings::TILE_SIZE, 1); if(t2 == nullptr || t2->id !="0"){ if(col_right_dist < dist_right) col_right_dist = dist_right; if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; ++col_right; } else{ if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; } } else{ if(col_right_dist < dist_right) col_right_dist = dist_right; ++col_right; } } else { //Chunk* c = map->getChunk(center2.x, center2.y - Settings::TILE_SIZE); Tile* t = map->getTile(center2.x, center2.y - Settings::TILE_SIZE, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x - Settings::TILE_SIZE, center2.y); Tile* t2 = map->getTile(center2.x - Settings::TILE_SIZE, center2.y, 1); if(t2 == nullptr || t2->id !="0"){ if(col_right_dist < dist_right) col_right_dist = dist_right; if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_right; ++col_bottom; } else{ if(col_right_dist < dist_right) col_right_dist = dist_right; ++col_right; } } else{ if(col_bottom_dist < dist_bottom) col_bottom_dist = dist_bottom; ++col_bottom; } } } //tercer quadrant else if(center1.x < center2.x && center1.y >= center2.y){ //std::cout << "tercer quadrant " << std::endl; float dist_right = right1 - left2; float dist_top = bottom2 - top1; if(dist_right <= dist_top){ //Chunk* c = map->getChunk(center2.x - Settings::TILE_SIZE, center2.y); Tile* t = map->getTile(center2.x - Settings::TILE_SIZE, center2.y, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x, center2.y + Settings::TILE_SIZE); Tile* t2 = map->getTile(center2.x, center2.y + Settings::TILE_SIZE, 1); if(t2 == nullptr || t2->id !="0"){ if(col_right_dist < dist_right) col_right_dist = dist_right; if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; ++col_right; } else{ if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; } } else{ if(col_right_dist < dist_right) col_right_dist = dist_right; ++col_right; } } else { //Chunk* c = map->getChunk(center2.x, center2.y + Settings::TILE_SIZE); Tile* t = map->getTile(center2.x, center2.y + Settings::TILE_SIZE, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x - Settings::TILE_SIZE, center2.y); Tile* t2 = map->getTile(center2.x - Settings::TILE_SIZE, center2.y, 1); if(t2 == nullptr || t2->id !="0"){ if(col_right_dist < dist_right) col_right_dist = dist_right; if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_right; ++col_top; } else{ if(col_right_dist < dist_right) col_right_dist = dist_right; ++col_right; } } else{ if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; } } } //quart quadrant else if(center1.x >= center2.x && center1.y > center2.y){ //std::cout << "quart quadrant " << std::endl; float dist_left = right2-left1; float dist_top = bottom2 - top1; if(dist_left <= dist_top){ //Chunk* c = map->getChunk(center2.x + Settings::TILE_SIZE, center2.y); Tile* t = map->getTile(center2.x + Settings::TILE_SIZE, center2.y, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x, center2.y + Settings::TILE_SIZE); Tile* t2 = map->getTile(center2.x, center2.y + Settings::TILE_SIZE, 1); if(t2 == nullptr || t2->id !="0"){ if(col_left_dist < dist_left) col_left_dist = dist_left; if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; ++col_left; } else{ if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; } }else{ if(col_left_dist < dist_left) col_left_dist = dist_left; ++col_left; } } else { //Chunk* c = map->getChunk(center2.x, center2.y + Settings::TILE_SIZE); Tile* t = map->getTile(center2.x, center2.y + Settings::TILE_SIZE, 1); if(t->id !="0"){ //Chunk* c2 = map->getChunk(center2.x + Settings::TILE_SIZE, center2.y); Tile* t2 = map->getTile(center2.x + Settings::TILE_SIZE, center2.y, 1); if(t2 == nullptr || t2->id !="0"){ if(col_left_dist < dist_left) col_left_dist = dist_left; if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_left; ++col_top; } else{ if(col_left_dist < dist_left) col_left_dist = dist_left; ++col_left; } }else{ if(col_top_dist < dist_top) col_top_dist = dist_top; ++col_top; } } } }
/* void testEmptyString( TestReporter& reporter ) { NEW_TEST_FUNCTION( reporter ); PangoTextInterface textInterface; STRING str = ""; MC2Point center( 0, 0 ); isab::Rectangle rect = textInterface.getStringAsRectangle( str, center, 0, 0, 0.0f ); CHECK( rect.getHeight() == 0, reporter ); CHECK( rect.getWidth() == 0, reporter ); CHECK( rect.getX() == center.getX(), reporter ); CHECK( rect.getY() == center.getY(), reporter ); std::vector<isab::Rectangle> boxes; int res = textInterface.getStringAsRectangles( boxes, str, center, 0, 0, 0.0f ); CHECK( res == 0, reporter ); MC2Point center2( -5, -5 ); isab::Rectangle rect2 = textInterface.getStringAsRectangle( str, center2, 0, 0, 0.0f); CHECK( rect2.getHeight() == 0, reporter ); CHECK( rect2.getWidth() == 0, reporter ); CHECK( rect2.getX() == center2.getX(), reporter ); CHECK( rect2.getY() == center2.getY(), reporter ); } */ void testSingleChar( TestReporter& reporter ) { NEW_TEST_FUNCTION( reporter ); PangoTextInterface textInterface; STRING str = "A"; /** * -- getStringAsRectangle * Check so that the position is smaller than the center coordinate * and the position + size is larger than the center coordinate. * */ MC2Point center( 0, 0 ); isab::Rectangle rect = textInterface.getStringAsRectangle( str, center, 0, 1, 0.0f ); CHECK( rect.getX() < center.getX(), reporter ); CHECK( ( rect.getX() + static_cast<int>( rect.getWidth() ) ) > center.getX(), reporter ); CHECK( rect.getY() < center.getY(), reporter ); CHECK( ( rect.getY() + static_cast<int>( rect.getHeight() ) ) > center.getY(), reporter ); /** * -- getStringAsRectangle * The same check as before, but with another center coordinate. * */ MC2Point center2( -5, -5 ); isab::Rectangle rect2 = textInterface.getStringAsRectangle( str, center2, 0, 1, 0.0f); CHECK( rect2.getX() < center2.getX(), reporter ); CHECK( ( rect2.getX() + static_cast<int>( rect2.getWidth() ) ) > center2.getX(), reporter ); CHECK( rect2.getY() < center2.getY(), reporter ); CHECK( ( rect2.getY() + static_cast<int>( rect2.getHeight() ) ) > center2.getY(), reporter ); /** * --getStringAsRectangles * Check so that only one rectangle is returned, and that * the position of this rectangle is smaller than the center * coordinate supplied, and that the position + size is larger * than the center coordinate supplied. * */ std::vector<isab::Rectangle> boxes; int res = textInterface.getStringAsRectangles( boxes, str, center, 0, 1, 0.0f ); CHECK( res == 1, reporter ); CHECK( boxes.size() == 1, reporter ); isab::Rectangle rect3 = boxes.at( 0 ); CHECK( rect3.getX() < center.getX(), reporter ); CHECK( ( rect3.getX() + static_cast<int>( rect3.getWidth() ) ) > center.getX(), reporter ); CHECK( rect3.getY() < center.getY(), reporter ); CHECK( ( rect3.getY() + static_cast<int>( rect3.getHeight() ) ) > center.getY(), reporter ); /** * --getStringAsRectangles * Same check as above, but with another center coordinate. * */ std::vector<isab::Rectangle> boxes2; int res2 = textInterface.getStringAsRectangles( boxes2, str, center2, 0, 1, 0.0f ); CHECK( res2 == static_cast<int>( strlen( str ) ), reporter ); CHECK( boxes2.size() == 1, reporter ); isab::Rectangle rect4 = boxes2.at( 0 ); CHECK( rect4.getX() < center2.getX(), reporter ); CHECK( ( rect4.getX() + static_cast<int>( rect4.getWidth() ) ) > center2.getX(), reporter ); CHECK( rect4.getY() < center2.getY(), reporter ); CHECK( ( rect4.getY() + static_cast<int>( rect4.getHeight() ) ) > center2.getY(), reporter ); }