void OctreeNode::subdivide(){ VoxelSize halfSize = getHalfSize(); if (halfSize > 1) { for (VoxelSize i = 0; i < 8; i++) { VoxelSize quarterSize = halfSize / 2; cout << "SIZE:" << +m_size << endl; Vector<3, VoxelSize> newOrigin({ VoxelSize(m_origin[Dimensions::X] + ((i & 4) ? quarterSize : -quarterSize)), VoxelSize(m_origin[Dimensions::Y] + ((i & 2) ? quarterSize : -quarterSize)), VoxelSize(m_origin[Dimensions::Z] + ((i & 1) ? quarterSize : -quarterSize)) }); m_children[i] = std::move(make_unique<OctreeNode>(m_block, getHalfSize(), newOrigin)); } } else { for (VoxelSize i = 0; i < 8; i++) { Vector<3, VoxelSize> newBottomLeft({ VoxelSize((i & 4) ? m_origin[Dimensions::X] : m_origin[Dimensions::X] - 1), VoxelSize((i & 2) ? m_origin[Dimensions::Y] : m_origin[Dimensions::Y] - 1), VoxelSize((i & 1) ? m_origin[Dimensions::Z] : m_origin[Dimensions::Z] - 1) }); m_children[i] = std::move(make_unique<OctreeNode>(m_block, getHalfSize(), newBottomLeft)); } } }
Vector3d DensityPlot::calcNewOrigin() { int i; Vector3d newOrigin(0.0); RealType totalMass = 0.0; for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL; sd = seleMan_.nextSelected(i)) { RealType mass = sd->getMass(); totalMass += mass; newOrigin += sd->getPos() * mass; } newOrigin /= totalMass; return newOrigin; }
void tSpriteBatch::draw(uint8_t z, tTexture* t, const tPoint2f& dest, const tOptional<tRectf>& src, const tColor4f& c, float rotz, const tPoint2f& origin, const tVector2f& scale) { tPoint2f newOrigin(int(origin.x * scale.x), int(origin.y * scale.y)); if (src) { draw(z, t, tRectf(dest, int(src->size.x * scale.x), int(src->size.y * scale.y)), src, c, rotz, newOrigin); } else { draw(z, t, tRectf(dest, int(t->getSurfaceSize().x * scale.x), int(t->getSurfaceSize().y * scale.y)), src, c, rotz, newOrigin); } }
DockShape::DockShape(sf::Shape **shapes, int numShapes, int left, int top, int width, int height, const char *hoverText, sf::Font *font, int fontSize, int textLeft, int textTop, const char *shortcut, int shortcutFontSize) : DockItem(left, top, width, height) { drawableShapes_ = shapes; drawables_ = (sf::Drawable**) shapes; numDrawables_ = numShapes; sf::Vector2f newOrigin(left_ + (width_ / 2), top_ + (height_ / 2)); for (int x = 0; x < numDrawables_; x++) { sf::Shape *shape = drawableShapes_[x]; shape->move(newOrigin); } hoverText_ = new sf::Text(hoverText, *font, fontSize); hoverText_->setPosition(textLeft, textTop); hoverText_->setColor(HIGHLIGHTED_COLOR); numHighlightedDrawables_ = numDrawables_ + 1; highlightedDrawables_ = new sf::Drawable*[numHighlightedDrawables_]; for (int x = 0; x < numDrawables_; x++) { highlightedDrawables_[x] = drawables_[x]; } highlightedDrawables_[numDrawables_] = hoverText_; shortcutText_ = new sf::Text(shortcut, *font, shortcutFontSize); sf::FloatRect shortcutRect = shortcutText_->getLocalBounds(); shortcutText_->setPosition(newOrigin.x - (shortcutRect.width / 2), top + height); shortcutText_->setColor(SHORTCUT_COLOR); numShortcutDrawables_ = numDrawables_ + 1; shortcutDrawables_ = new sf::Drawable*[numShortcutDrawables_]; for (int x = 0; x < numDrawables_; x++) { shortcutDrawables_[x] = drawables_[x]; } shortcutDrawables_[numDrawables_] = shortcutText_; }
void sKeyCallback(GLFWwindow*, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { switch (key) { case GLFW_KEY_ESCAPE: // Quit glfwSetWindowShouldClose(mainWindow, GL_TRUE); break; case GLFW_KEY_LEFT: // Pan left if (mods == GLFW_MOD_CONTROL) { b2Vec2 newOrigin(2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.x -= 0.5f; } break; case GLFW_KEY_RIGHT: // Pan right if (mods == GLFW_MOD_CONTROL) { b2Vec2 newOrigin(-2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.x += 0.5f; } break; case GLFW_KEY_DOWN: // Pan down if (mods == GLFW_MOD_CONTROL) { b2Vec2 newOrigin(0.0f, 2.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.y -= 0.5f; } break; case GLFW_KEY_UP: // Pan up if (mods == GLFW_MOD_CONTROL) { b2Vec2 newOrigin(0.0f, -2.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.y += 0.5f; } break; case GLFW_KEY_HOME: // Reset view g_camera.m_zoom = 1.0f; g_camera.m_center.Set(0.0f, 20.0f); break; case GLFW_KEY_Z: // Zoom out g_camera.m_zoom = b2Min(1.1f * g_camera.m_zoom, 20.0f); break; case GLFW_KEY_X: // Zoom in g_camera.m_zoom = b2Max(0.9f * g_camera.m_zoom, 0.02f); break; case GLFW_KEY_R: // Reset test delete test; test = entry->createFcn(b2d_scripts[script_index].filepath); break; case GLFW_KEY_SPACE: // Launch a bomb. if (test) { test->LaunchBomb(); } break; case GLFW_KEY_P: // Pause settings.pause = !settings.pause; break; /* case GLFW_KEY_LEFT_BRACKET: // Switch to previous test --testSelection; if (testSelection < 0) { testSelection = testCount - 1; } break; case GLFW_KEY_RIGHT_BRACKET: // Switch to next test ++testSelection; if (testSelection == testCount) { testSelection = 0; } break; */ case GLFW_KEY_TAB: ui.showMenu = !ui.showMenu; default: if (test) { test->Keyboard(key); } } } else if (action == GLFW_RELEASE) { test->KeyboardUp(key); } // else GLFW_REPEAT }
static void KeyboardSpecial(int key, int x, int y) { B2_NOT_USED(x); B2_NOT_USED(y); int mod = glutGetModifiers(); switch (key) { // Press left to pan left. case GLUT_KEY_LEFT: if (mod == GLUT_ACTIVE_CTRL) { b2Vec2 newOrigin(2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { settings.viewCenter.x -= 0.5f; Resize(width, height); } break; // Press right to pan right. case GLUT_KEY_RIGHT: if (mod == GLUT_ACTIVE_CTRL) { b2Vec2 newOrigin(-2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { settings.viewCenter.x += 0.5f; Resize(width, height); } break; // Press down to pan down. case GLUT_KEY_DOWN: if (mod == GLUT_ACTIVE_CTRL) { b2Vec2 newOrigin(0.0f, 2.0f); test->ShiftOrigin(newOrigin); } else { settings.viewCenter.y -= 0.5f; Resize(width, height); } break; // Press up to pan up. case GLUT_KEY_UP: if (mod == GLUT_ACTIVE_CTRL) { b2Vec2 newOrigin(0.0f, -2.0f); test->ShiftOrigin(newOrigin); } else { settings.viewCenter.y += 0.5f; Resize(width, height); } break; // Press home to reset the view. case GLUT_KEY_HOME: viewZoom = 1.0f; settings.viewCenter.Set(0.0f, 20.0f); Resize(width, height); break; } }
__host__ __device__ bool intersect(Ray& ray) const { switch(type) { case SPHERE: { float radius = parameter.data[1].x; const Vector3Df& center = parameter.data[0]; float boundingSquare = radius * radius; Vector3Df newOrigin(ray.getOrigin().x - center.x, ray.getOrigin().y - center.y, ray.getOrigin().z - center.z); float a, b, c; a = ray.getDirection() * ray.getDirection(); b = 2 * (newOrigin * ray.getDirection()); c = newOrigin * newOrigin - boundingSquare; float t1, t2; int roots = calcQuadricRoots (a, b, c, t1, t2); if(t1 < 0) { t1 = t2; } if (roots > 0) { if(t1 > 0 && t1 < ray.getT()) { ray.setT(t1); ray.setObject((Renderable*) this); return true; } } return false; } case PLANE: { const Vector3Df& planeNormal = -parameter.data[0]; const Vector3Df& pointOnPlane = parameter.data[1]; float denom = planeNormal * ray.getDirection(); if(denom > 1e-6) { Vector3Df p0l0 = pointOnPlane - ray.getOrigin(); float t = (p0l0 * planeNormal) / denom; if(t > 0 && t < ray.getT()) { ray.setT(t); ray.setObject((Renderable*) this); return true; } } return false; } } return false; }
/** * Main program * @param argc Number of command line arguments, inlucing the program itself. * @param argv Vector of command line arguments. * @return EXIT_SUCCESS if program exits normally, EXIT_ERROR otherwise. */ int main(int argc, char** argv) { if(argc < 2 || argc > 3) { std::cout << "Usage: warper input_image [ouput_image]" << std::endl; return EXIT_FAILURE; } readImage(argv[1]); Matrix3x3 M(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0); process_input(M, originalWidth, originalHeight); Vector3d upperRight(originalWidth-1,originalHeight-1, 1); Vector3d lowerRight(originalWidth-1,0, 1); Vector3d upperLeft(0,originalHeight-1, 1); Vector3d lowerLeft(0,0, 1); upperRight = (M * upperRight)/upperRight[2]; lowerRight = (M * lowerRight)/lowerRight[2]; upperLeft = (M * upperLeft)/upperLeft[2]; lowerLeft = (M * lowerLeft)/lowerLeft[2]; newWidth = max(max(lowerLeft[0], lowerRight[0]), max(upperLeft[0], upperRight[0])); newHeight = max(max(lowerLeft[1], lowerRight[1]), max(upperLeft[1], upperRight[1])); int originX = min(min(lowerLeft[0], lowerRight[0]), min(upperLeft[0], upperRight[0])); int originY = min(min(lowerLeft[1], upperLeft[1]), min(lowerRight[1], upperRight[1])); newHeight = newHeight - originY; newWidth = newWidth - originX; Vector3d newOrigin(originX, originY, 0); // Initalize 2d array warppedPixels = new rgba_pixel*[newHeight]; warppedPixels[0] = new rgba_pixel[newWidth*newHeight]; for (int i=1; i < newHeight; i++) { warppedPixels[i] = warppedPixels[i-1] + newWidth; } Matrix3x3 invM = M.inv(); for(int row = 0; row < newHeight; row++) for(int col = 0; col < newWidth; col++) { Vector3d pixel_out(col, row, 1); pixel_out = pixel_out + newOrigin; Vector3d pixel_in = invM * pixel_out; float u = pixel_in[0] / pixel_in[2]; float v = pixel_in[1] / pixel_in[2]; int roundedU = round(u); int roundedV = round(v); if((0 <= roundedU && roundedU < originalWidth) && (0 <= roundedV && roundedV < originalHeight)) { warppedPixels[row][col] = pixels[roundedV][roundedU]; } else { rgba_pixel p; p.r = 0; p.g = 0; p.b = 0; p.a = 1; warppedPixels[row][col] = p; } } // Flip for openGL openGLFlip(); // Init OpenGL glutInit(&argc, argv); openGLSetup(newWidth, newHeight); if(argc == 3) { outImage = argv[2]; writeImage(); } // Start running display window glutMainLoop(); return EXIT_SUCCESS; }
cv::Mat TestProjection::test(double userX, double userY, double userZ, const char* filename) { //Coordinates of the projection in the real world /*cv::Point3f p11(-480, 735, -420); cv::Point3f p12(0, 935, 0); cv::Point3f p13(0, 220, 0); cv::Point3f p14(-480, 240, -420); Plane3d proj1(p11, p12, p13, p14); cv::Point3f p21(0, 935, 0); cv::Point3f p22(480, 735, -420); cv::Point3f p23(480, 240, -420); cv::Point3f p24(0, 220, 0); Plane3d proj2(p21, p22, p23, p24);*/ cv::Point3f p11(-590, 725, -350); cv::Point3f p12(0, 955, 0); cv::Point3f p13(0, 200, 0); cv::Point3f p14(-590, 227, -350); Plane3d proj1(p11, p12, p13, p14); cv::Point3f p21(0, 955, 0); cv::Point3f p22(567, 755, -350); cv::Point3f p23(567, 227, -350); cv::Point3f p24(0, 200, 0); Plane3d proj2(p21, p22, p23, p24); std::vector<Plane3d> planes; planes.push_back(proj1); planes.push_back(proj2); Projection proj(planes); // proj.print(); //Create the user with the obtained projection coordinates User u(proj); //Update his position u.updatePosition(userX, userY, userZ); // u.print(); //Create the distorted-corrected plane pairs, using the projections //on the user's view plane //Plane 1 //**************************************************************************************************** Plane2d p1 = u.getProjectedPlanes().at(0).to2d(); Plane2d p2(cv::Point2f(0, 0), cv::Point2f(480, 0), cv::Point2f(480, 540), cv::Point2f(0, 540)); // Plane2d p2(cv::Point2f(0, 0), cv::Point2f(230, 0), cv::Point2f(230, 520), cv::Point2f(0, 520)); // Plane2d p2(cv::Point2f(0, 0), cv::Point2f(270, 0), cv::Point2f(270, 405), cv::Point2f(0, 405)); //**************************************************************************************************** //Invert the plane y coordinates Plane2d inv1 = p1.yInverted(); //Move it so that it's closer to the target plane cv::Vec2f dist = pjs::distance(inv1, p2); Plane2d pp1(cv::Point2f(inv1.getPoint(0).x - dist[0], inv1.getPoint(0).y - dist[1]), cv::Point2f(inv1.getPoint(1).x - dist[0], inv1.getPoint(1).y - dist[1]), cv::Point2f(inv1.getPoint(2).x - dist[0], inv1.getPoint(2).y - dist[1]), cv::Point2f(inv1.getPoint(3).x - dist[0], inv1.getPoint(3).y - dist[1])); //Plane 2 //**************************************************************************************************** Plane2d p3 = u.getProjectedPlanes().at(1).to2d(); Plane2d p4(cv::Point2f(0, 0), cv::Point2f(480, 0), cv::Point2f(480, 540), cv::Point2f(0, 540)); // Plane2d p4(cv::Point2f(0, 0), cv::Point2f(230, 0), cv::Point2f(230, 520), cv::Point2f(0, 520)); // Plane2d p4(cv::Point2f(0, 0), cv::Point2f(270, 0), cv::Point2f(270, 405), cv::Point2f(0, 405)); //**************************************************************************************************** //Invert the plane y coordinates Plane2d inv2 = p3.yInverted(); //Move it so that it's closer to the target plane dist = pjs::distance(inv2, p4); Plane2d pp3(cv::Point2f(inv2.getPoint(0).x - dist[0], inv2.getPoint(0).y - dist[1]), cv::Point2f(inv2.getPoint(1).x - dist[0], inv2.getPoint(1).y - dist[1]), cv::Point2f(inv2.getPoint(2).x - dist[0], inv2.getPoint(2).y - dist[1]), cv::Point2f(inv2.getPoint(3).x - dist[0], inv2.getPoint(3).y - dist[1])); //*********************** //Load the target image //*********************** cv::Mat img = cv::imread(filename, CV_LOAD_IMAGE_COLOR); if (!img.data) { std::cout << " --(!) Error reading image" << std::endl; throw std::exception(); } //Helper object Utils utils; //Divide the image in two // std::vector<cv::Mat> images = utils.divideImageInTwo(img); //Build the surfaces with their reference planes and their corresponding //image Surface s1(pp1, p2); Surface s2(pp3, p4); std::vector<Surface*> surfaces; surfaces.push_back(&s1); surfaces.push_back(&s2); int originX; int padding; int screenWidth = 1280; int screenHeight = 800; //TODO recursive position correction int width1 = s1.getWidth(); int width2 = s2.getWidth(); int diffW = width1 - width2; if (diffW < 0) { originX = screenWidth / 2 - width1; padding = 0; } else { originX = 0 + screenWidth / 2 - width1; padding = 0; } //1st position correction cv::Point2f origin(originX, 0); s1.correctBBPosition(origin); cv::Point2f s1ur = s1.getUpperRightCorner(); s2.correctPosition(s1ur); cv::Point2f upperLeft = s2.getUpperLeftCorner(); cv::Point2f upperRight = s2.getUpperRightCorner(); double topY; if (upperLeft.y < upperRight.y) { topY = upperLeft.y; } else { topY = upperRight.y; } cv::Size size = utils.getFinalSize(surfaces); int diffH = screenHeight - size.height; //2nd position correction if necessary (if second plane is still outside) if (!topY < 0) { topY = 0; } cv::Point2f newOrigin(originX, -topY + diffH / 2); s1.correctBBPosition(newOrigin); s1ur = s1.getUpperRightCorner(); s2.correctPosition(s1ur); // cv::Size size = utils.getFinalSize(surfaces); size.width += padding; size.width = std::max(screenWidth, size.width); size.height = screenHeight; cv::Size sizeS1(size.width / 2, size.height); s1.setSize(sizeS1); s2.setSize(size); std::vector<cv::Mat> images = utils.divideImageInTwo(img); s1.setImage(images.at(0)); s2.setImage(images.at(1)); s1.applyHomography(); s2.applyHomography(); // s1.addTransparency(); // s2.addTransparency(); cv::Mat finalImage = utils.getImageFromSurfaces(surfaces); surfaces.clear(); return finalImage; }
void __fastcall TTestBedForm::FormKeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar, TShiftState Shift) { bool modCtrl = Shift.Contains(ssCtrl); switch(Key) { case vkEscape: // Quit Close(); break; case vkLeft: // Pan left if (modCtrl && test) { b2Vec2 newOrigin(2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.x -= 0.5f; } break; case vkRight: // Pan right if (modCtrl && test) { b2Vec2 newOrigin(-2.0f, 0.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.x += 0.5f; } break; case vkDown: // Pan down if (modCtrl && test) { b2Vec2 newOrigin(0.0f, 2.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.y -= 0.5f; } break; case vkUp: // Pan up if (modCtrl && test) { b2Vec2 newOrigin(0.0f, -2.0f); test->ShiftOrigin(newOrigin); } else { g_camera.m_center.y += 0.5f; } break; case vkHome: // Reset view g_camera.m_zoom = 1.0f; g_camera.m_center.Set(0.0f, 20.0f); break; case vkSpace: // Launch a bomb. if (test) { test->LaunchBomb(); } break; default: switch (KeyChar) { case 'P': case 'p': // Pause settings.pause = !settings.pause; break; case ' ': // Launch a bomb. if (test) { test->LaunchBomb(); } break; case 'R': case 'r': // Reset test delete test; test = entry->createFcn(); break; default: if (test) { test->Keyboard(static_cast<int>(ToUpper(KeyChar))); } } } }