DgIVec2D& DgBoundedRF2D::incrementAddress (DgIVec2D& add) const { if (!validAddress(add)) return add = invalidAdd(); else if (add == upperRight() || add == endAdd()) return add = endAdd(); if (add.j() == upperRight().j()) { add = DgIVec2D(add.i() + 1, lowerLeft().j()); } else { add.setJ(add.j() + 1); } return add; } // DgIVec2D& DgBoundedRF2D::incrementAddress
std::auto_ptr<Bempp::Grid> SimpleTriangularGridManager::createGrid() { Bempp::GridParameters params; params.topology = Bempp::GridParameters::TRIANGULAR; const int dimGrid = 2; arma::Col<Bempp::ctype> lowerLeft(dimGrid); arma::Col<Bempp::ctype> upperRight(dimGrid); arma::Col<unsigned int> nElements(dimGrid); lowerLeft.fill(0); upperRight.fill(1); nElements(0) = N_ELEMENTS_X; nElements(1) = N_ELEMENTS_Y; return Bempp::GridFactory::createStructuredGrid(params, lowerLeft, upperRight, nElements); }
DgIVec2D& DgBoundedRF2D::decrementAddress (DgIVec2D& add) const { if (!validAddress(add) || add == lowerLeft()) return add = invalidAdd(); if (add.j() == lowerLeft().j()) { add = DgIVec2D(add.i() - 1, upperRight().j()); } else { add.setJ(add.j() - 1); } return add; } // DgIVec2D& DgBoundedRF2D::decrementAddress
bool LineIntersectsRect( const DVec2 &v1, const DVec2 &v2, float x, float y, float width, float height ) { FVec2 lowerLeft( x, y+height ); FVec2 upperRight( x+width, y ); FVec2 upperLeft( x, y ); FVec2 lowerRight( x+width, y+height); // check if it is inside if (v1.x > lowerLeft.x && v1.x < upperRight.x && v1.y < lowerLeft.y && v1.y > upperRight.y && v2.x > lowerLeft.x && v2.x < upperRight.x && v2.y < lowerLeft.y && v2.y > upperRight.y ) { return true; } // check each line for intersection if (LineIntersectLine(v1,v2, upperLeft, lowerLeft ) ) return true; if (LineIntersectLine(v1,v2, lowerLeft, lowerRight) ) return true; if (LineIntersectLine(v1,v2, upperLeft, upperRight) ) return true; if (LineIntersectLine(v1,v2, upperRight, lowerRight) ) return true; 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; }
bool Ball::FlipperCollision(Vector4& otherCenter, Game_Object& other, float& angle) { bool hasCollided; Vector4 axis, C, A, B; float projC, projA, projB, gap; // Calculate the center Vector2 thisCenter(this->translationMatrix[0][3], this->translationMatrix[1][3]); // Corners of flipper Vector4 upperRight(other.sprite->GetRadius().x, other.sprite->GetRadius().y, 0.0f, 0.0f); Vector4 upperLeft(-other.sprite->GetRadius().x, other.sprite->GetRadius().y, 0.0f, 0.0f); Vector4 bottomRight(other.sprite->GetRadius().x, -other.sprite->GetRadius().y, 0.0f, 0.0f); Vector4 bottomLeft(-other.sprite->GetRadius().x, -other.sprite->GetRadius().y, 0.0f, 0.0f); // Properly rotate all corners upperRight = other.rotationMatrix * upperRight; upperLeft = other.rotationMatrix * upperLeft; bottomRight = other.rotationMatrix * bottomRight; bottomLeft = other.rotationMatrix * bottomLeft; // Corners of circle Vector4 circleUpperRight( (float)sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), (float)sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), 0.0f, 0.0f ); Vector4 circleUpperLeft( (float)-sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), (float)sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), 0.0f, 0.0f ); Vector4 circleBottomRight( (float)sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), (float)-sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), 0.0f, 0.0f ); Vector4 circleBottomLeft( (float)-sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), (float)-sqrt( pow( this->sprite->GetRadius().x, 2.0f ) / 2.0f ), 0.0f, 0.0f ); // Properly rotate all corners circleUpperRight = other.rotationMatrix * circleUpperRight; circleUpperLeft = other.rotationMatrix * circleUpperLeft; circleBottomRight = other.rotationMatrix * circleBottomRight; circleBottomLeft = other.rotationMatrix * circleBottomLeft; // Axis 1 (top) // Calculate axis, 3 vectors, the dot, and then the gap axis = Vector4( (upperRight - upperLeft).normalize(upperRight - upperLeft) ); C = Vector4(otherCenter.x - thisCenter.x, otherCenter.y - thisCenter.y, 0.0f, 0.0f); A = Vector4(circleUpperRight); B = Vector4(upperRight); projC = C.dot(axis); projA = A.dot(axis); projB = B.dot(axis); gap = abs(projC) - abs(projA) - abs(projB); // Check value of gap if(gap > 0) return false; // Axis 2 (bottom) // Calculate axis, 3 vectors, the dot, and then the gap axis = Vector4( (bottomRight - bottomLeft).normalize(bottomRight - bottomLeft) ); C = Vector4(otherCenter.x - thisCenter.x, otherCenter.y - thisCenter.y, 0.0f, 0.0f); A = Vector4(circleBottomRight); B = Vector4(bottomRight); projC = C.dot(axis); projA = A.dot(axis); projB = B.dot(axis); gap = abs(projC) - abs(projA) - abs(projB); // Check value of gap if(gap > 0) return false; // Axis 3 (right) // Calculate axis, 3 vectors, the dot, and then the gap axis = Vector4( (upperRight - bottomRight).normalize(upperRight - bottomRight) ); C = Vector4(otherCenter.x - thisCenter.x, otherCenter.y - thisCenter.y, 0.0f, 0.0f); A = Vector4(circleBottomRight); B = Vector4(bottomRight); projC = C.dot(axis); projA = A.dot(axis); projB = B.dot(axis); gap = abs(projC) - abs(projA) - abs(projB); // Check value of gap if(gap > 0) return false; // Axis 4 (left) // Calculate axis, 3 vectors, the dot, and then the gap axis = Vector4( (upperLeft - bottomLeft).normalize(upperLeft - bottomLeft) ); C = Vector4(otherCenter.x - thisCenter.x, otherCenter.y - thisCenter.y, 0.0f, 0.0f); A = Vector4(circleBottomLeft); B = Vector4(bottomLeft); projC = C.dot(axis); projA = A.dot(axis); projB = B.dot(axis); gap = abs(projC) - abs(projA) - abs(projB); // Check value of gap if(gap > 0) return false; float currentAngle = acos(other.rotationMatrix[0][0]); // Collides with long side if(projC > 0 && projA < 0 ) { // compare against leftUpper && leftLower // use distance formula - from circle_center to each point // which is shortest? leftUpper => "above", leftLower => "below" float distanceUpper = ( (upperLeft + otherCenter) - Vector4(thisCenter.x, thisCenter.y, 0.0f, 0.0f )).length(); float distanceLower = ( (bottomLeft + otherCenter) - Vector4(thisCenter.x, thisCenter.y, 0.0f, 0.0f )).length(); if(distanceUpper < distanceLower) { // Hit "top" side angle = currentAngle; if(angle <= PI) angle = PI - angle; else angle = 2*PI - angle; } else { // Hit "bottom" side angle = currentAngle + PI; if(angle <= PI) angle = PI - angle; else angle = 2*PI - angle; } } // Collides with short side else { // compare against leftUpper && rightUpper // use distance formula - from circle_center to each point // which is shortest? leftUpper => "left", rightUpper => "right" float distanceLeft = ( (upperLeft + otherCenter) - Vector4(thisCenter.x, thisCenter.y, 0.0f, 0.0f )).length(); float distanceRight = ( (upperRight + otherCenter) - Vector4(thisCenter.x, thisCenter.y, 0.0f, 0.0f )).length(); if(distanceLeft < distanceRight) { // Hit "left" side angle = currentAngle + PI / 2; if(angle <= PI) angle = PI - angle; else angle = 2*PI - angle; } else { // Hit "right" side angle = currentAngle + 3 * PI / 2; if(angle <= PI) angle = PI - angle; else angle = 2*PI - angle; } } return true; }