void symbolicSample() { std::string ans; char rowChar[5]; int rowTmp = ROW; sprintf(rowChar, "%d", rowTmp); std::string row = rowChar; /** Create a variable X and an identity function */ symbolic_matrix_type X("X", ROW, COL); SymbolicMMFunc fX(X, false); /** Create constants A,B and C and identity functions */ symbolic_matrix_type A("A", ROW, COL); SymbolicMMFunc fA(A, true); /** Scalar-matrix function placeholder */ SymbolicSMFunc func; /** Create the scalar-matrix function. */ func = trace (fA * inv(fX)); /** Output the function value and derivative value. */ std::cout << "Function Value: "<<func.functionVal.getString()<<std::endl; std::cout << "Derivative Value: "<<func.derivativeVal.getString()<<std::endl; AMD::SymbolicScalarMatlab num("10"); SymbolicSMFunc constant1(num, ROW, COL); SymbolicSMFunc constant2(num, ROW, COL); SymbolicSMFunc constant3(num, ROW, COL); func = trace(constant3*fA*inv(fX)) + constant1 + constant2; std::cout << "Function Value: " << func.functionVal.getString() << std::endl; std::cout << "Derivative Vale: " << func.derivativeVal.getString() << std::endl; }
/** An sample for taylor expansion of logdet(X). */ void taylorSample() { std::string ans; char rowChar[5]; int rowTmp = ROW; sprintf(rowChar, "%d", rowTmp); std::string row = rowChar; // Initialize the matrices. symbolic_matrix_type X("X", ROW, COL); symbolic_matrix_type X0("X0", ROW, COL); symbolic_matrix_type Delta("(X-X0)", ROW, COL); AMD::SymbolicScalarMatlab a2("1/2!"); AMD::SymbolicScalarMatlab a3("1/3!"); SymbolicSMFunc r2(a2,ROW,COL); SymbolicSMFunc r3(a3,ROW, COL); // Initialize MatrixMatrixFunction. SymbolicMMFunc fX(X, false); SymbolicMMFunc fX0(X0, false); SymbolicMMFunc fDelta(Delta, true); // Compute Taylor series iteratively. SymbolicSMFunc f0 = logdet(fX0); SymbolicSMFunc f1 = trace(fDelta * transpose(*f0.derivativeFuncVal)); SymbolicSMFunc f2 = trace(fDelta * transpose(*f1.derivativeFuncVal)); SymbolicSMFunc f3 = trace(fDelta * transpose(*f2.derivativeFuncVal)); // Taylor Expansion. SymbolicSMFunc func = f0 + f1 + r2*f2 + r3*f3; std::cout<<"The first 4 terms of Taylor Expansion for logdet(X) around X0 is:"; std::cout << std::endl; std::cout << func.functionVal.getString() << std::endl; }
void Font::drawSquare (float size) { MGL_DATATYPE texCoords[8] = { 0,0, fX(1),0, 0,fX(1), fX(1),fX(1), }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D,tex); glPushMatrix(); glScalef(size, size, 1); glVertexPointer(2, MGL_TYPE, 0, squareVerts); glTexCoordPointer(2, MGL_TYPE, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); }
template<int m> static void perturbed_sign_test() { for (const int power : vec(1,2,3)) for (const int index : range(20)) { if (verbose) cout << endl; // Evaluate perturbed sign using our fancy routine nasty_power = power; nasty_index = index; Array<exact::Perturbed<m>> fX(1); fX[0].seed_ = index; const bool fast = perturbed_sign<exact::Perturbed<m>>(nasty_predicate,m*power,fX); GEODE_ASSERT((power&1) || fast); // Evaluate the series out to several terms using brute force Array<int> powers(m+1); // Choose powers of 2 to approximate nested infinitesimals for (int i=0;i<m;i++) powers[i+1] = (power+1)*powers[i]+128; mpz_t yp; mpz_init(yp); const int enough = 5120/(8*sizeof(ExactInt)); Vector<Exact<enough>,m> sX[1]; for (int i=0;i<=m+1;i++) { if (i) { const Vector<Exact<1>,m> y(perturbation<m>(i,index)); for (int j=0;j<m;j++) { auto& x = sX[0][j]; Exact<enough> yp; const int skip = (powers.back()-powers[i-1])/(8*sizeof(mp_limb_t)); mpz_set(asarray(yp.n).slice(skip,yp.limbs),y[j]); // yp = y[j]<<(powers[-1]-powers[i-1]) x += yp; } } // We should be initially zero, and then match the correct sign once nonzero Array<mp_limb_t> result(enough*m*power,uninit); nasty_predicate(result,asconstarray(sX)); const int slow = mpz_sign(result); if (0) { cout << "m "<<m<<", power "<<power<<", index "<<index<<", i "<<i<<", fast "<<2*fast-1<<", slow "<<slow<<endl; cout << " fX = "<<fX[0]<<", sX = "<<sX[0]<<" (x = "<<mpz_str(asarray(sX[0].x.n),true)<<')'<<endl; cout << " sX result = "<<mpz_str(result,true)<<endl; } GEODE_ASSERT(slow==(i<m?0:2*fast-1)); } mpz_clear(yp); } }
// vMarchCube1 performs the Marching Cubes algorithm on a single cube GLvoid MarchingCubes::vMarchCube1(const GLint &iX, const GLint &iY, const GLint &iZ, const GLfloat& isoValue) { GLfloat fX(iX*m_stepSize); GLfloat fY(iY*m_stepSize); GLfloat fZ(iZ*m_stepSize); GLint iCorner, iVertex, iVertexTest, iEdge, iTriangle, iFlagIndex, iEdgeFlags; GLfloat fOffset; GLfloat afCubeValue[8]; GLvector asEdgeVertex[12]; GLvector asEdgeNorm[12]; // Make a local copy of the values at the cube's corners for (iVertex = 0; iVertex < 8; iVertex++) { afCubeValue[iVertex] = (*m_grid)(iX + s_vertexIndexOffset[iVertex][0], iY + s_vertexIndexOffset[iVertex][1], iZ + s_vertexIndexOffset[iVertex][2]); } // Find which vertices are inside of the surface and which are outside iFlagIndex = 0; for (iVertexTest = 0; iVertexTest < 8; iVertexTest++) { if (afCubeValue[iVertexTest] <= isoValue) iFlagIndex |= 1<<iVertexTest; } // Find which edges are intersected by the surface iEdgeFlags = s_cubeEdgeFlags[iFlagIndex]; // If the cube is entirely inside or outside of the surface, then there will // be no intersections if(iEdgeFlags == 0) return; // Find the point of intersection of the surface with each edge // Then find the normal to the surface at those points for (iEdge = 0; iEdge < 12; iEdge++) { // if there is an intersection on this edge if (iEdgeFlags & (1<<iEdge)) { fOffset = getOffset(afCubeValue[ s_edgeConnection[iEdge][0] ], afCubeValue[ s_edgeConnection[iEdge][1] ], isoValue); asEdgeVertex[iEdge].fX = fX + (s_vertexOffset[ s_edgeConnection[iEdge][0] ][0] + fOffset * s_edgeDirection[iEdge][0]) * m_stepSize; asEdgeVertex[iEdge].fY = fY + (s_vertexOffset[ s_edgeConnection[iEdge][0] ][1] + fOffset * s_edgeDirection[iEdge][1]) * m_stepSize; asEdgeVertex[iEdge].fZ = fZ + (s_vertexOffset[ s_edgeConnection[iEdge][0] ][2] + fOffset * s_edgeDirection[iEdge][2]) * m_stepSize; surfaceNormal(asEdgeNorm[iEdge], asEdgeVertex[iEdge].fX, asEdgeVertex[iEdge].fY, asEdgeVertex[iEdge].fZ); if (isoValue < 0) { asEdgeNorm[iEdge].fX = - asEdgeNorm[iEdge].fX; asEdgeNorm[iEdge].fY = - asEdgeNorm[iEdge].fY; asEdgeNorm[iEdge].fZ = - asEdgeNorm[iEdge].fZ; } } } // Draw the triangles that were found. There can be up to five per cube for (iTriangle = 0; iTriangle < 5; iTriangle++) { if (s_triangleConnectionTable[iFlagIndex][3*iTriangle] < 0) break; for (iCorner = 0; iCorner < 3; iCorner++) { iVertex = s_triangleConnectionTable[iFlagIndex][3*iTriangle+iCorner]; m_surfaceData.push_back(asEdgeNorm[iVertex].fX); m_surfaceData.push_back(asEdgeNorm[iVertex].fY); m_surfaceData.push_back(asEdgeNorm[iVertex].fZ); m_surfaceData.push_back(asEdgeVertex[iVertex].fX); m_surfaceData.push_back(asEdgeVertex[iVertex].fY); m_surfaceData.push_back(asEdgeVertex[iVertex].fZ); } } }
namespace fontlib { static MGL_DATATYPE squareVerts[8] = { /* fX(-0.5), fX(-0.5), 0, fX(0.5), fX(-0.5), 0, fX(-0.5), fX(0.5), 0, fX(0.5), fX(0.5), 0*/ 0,0, fX(1),0, 0,fX(1), fX(1),fX(1) }; Font::Font (const GLuint atlasTex, const map<unsigned long, Glyph>& glyphMap, float glyphSize) : tex(atlasTex), glyphMap(glyphMap), glyphSize(glyphSize) { } void Font::drawSquare (float size) { MGL_DATATYPE texCoords[8] = { 0,0, fX(1),0, 0,fX(1), fX(1),fX(1), }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D,tex); glPushMatrix(); glScalef(size, size, 1); glVertexPointer(2, MGL_TYPE, 0, squareVerts); glTexCoordPointer(2, MGL_TYPE, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } void Font::drawGlyph (const Glyph& gi) { glPushMatrix(); glTranslatef(gi.leftMargin, gi.topMargin, 0.0f); float texCoords[8] = { gi.atlasX, gi.atlasY, gi.atlasX+glyphSize, gi.atlasY, gi.atlasX, gi.atlasY+glyphSize, gi.atlasX+glyphSize, gi.atlasY+glyphSize }; glVertexPointer(2, MGL_TYPE, 0, squareVerts); glTexCoordPointer(2, GL_FLOAT, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); } void Font::draw (const char* str, float size, bool autoGLState) { glBindTexture(GL_TEXTURE_2D, tex); if (autoGLState) { glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); } glPushMatrix(); glScalef(size, size, 1.0f); float advanceAccum = 0; for (const char* c = str; *c!='\0'; c++) { if (*c == '\n') { glTranslatef(-advanceAccum, 1, 0); advanceAccum = 0; } else { const Glyph& gi = glyphMap.get((unsigned long)*c); drawGlyph(gi); glTranslatef(gi.advance, 0, 0); advanceAccum += gi.advance; } } if (autoGLState) { glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glPopMatrix(); } void Font::getExtent (const char* str, float size, float* w, float* h) { float longestLine = 0; //longest line length int numLines = 1; float currLineLength = 0; for (const char* c = str; *c!='\0'; c++) { if (*c == '\n') { numLines++; if (currLineLength > longestLine) longestLine = currLineLength; currLineLength = 0; } else { const Glyph& gi = glyphMap.get((unsigned long)*c); currLineLength += glyphSize*size + gi.leftMargin*size + gi.advance*size; } } if (currLineLength > longestLine) longestLine = currLineLength; *w = longestLine; *h = numLines*glyphSize*size; } }
#include "Square.h" static MGL_DATATYPE verts[12] = { fX(-0.5), fX(-0.5), 0, fX(0.5), fX(-0.5), 0, fX(-0.5), fX(0.5), 0, fX(0.5), fX(0.5), 0 /*0,0,0, fX(1.0), 0,0, 0,fX(1.0),0, fX(1.0),fX(1.0),0*/ }; static MGL_DATATYPE texCoords[8] = { 0, fX(1), fX(1), fX(1), 0,0, fX(1),0 }; static uint8_t lineIndices[4] = { 0,1,3,2 }; void Square::create () { //FIXME: Create VBOs } void Square::draw (bool enableTexture) { glVertexPointer(3, MGL_TYPE, 0, verts);
void Meteor::drawTexture() { b2Vec2 position = body->GetPosition(); int texture = tex.getTex(); // LOGI("draw image (%i)", texture); #define fX(x) ((int)(x * (1 << 16))) int square[12] = { fX(-0.5), fX(-0.5), 0, fX(0.5), fX(-0.5), 0, fX(-0.5), fX(0.5), 0, fX(0.5), fX(0.5), 0 }; int texCoords[8] = { 0, fX(1), fX(1), fX(1), 0,0, fX(1),0 }; glPushMatrix(); glBindTexture( GL_TEXTURE_2D, texture ); glTranslatef(position.x, position.y, 0); glScalef(20,20,0); glVertexPointer(3, GL_FIXED, 0, square); glTexCoordPointer(2, GL_FIXED, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); }
int main(int argc, char *argv[]) { # include "setRootCase.H" # include "createTime.H" instantList Times = runTime.times(); # include "checkTimeOptions.H" runTime.setTime(Times[startTime],startTime); # include "createMesh.H" typedef VectorSpace<Vector<scalar>,scalar,4> quad; typedef VectorSpace<Vector<scalar>,scalar,6> hex; //read setDiscreteFieldsDictionary IOdictionary setDiscreteFieldsDict ( IOobject ( "setDiscreteFieldsDict", runTime.system(), mesh, IOobject::MUST_READ, IOobject::NO_WRITE ) ); // read pointer lists of each "Fields" PtrList<entry> parts=setDiscreteFieldsDict.lookup("Fields"); forAll(parts,partI) { const dictionary &part=parts[partI].dict(); Info <<part <<endl; // "field" ,"type", and "profile" are required in setDiscreteFieldsDict word field=part["field"]; word type=part["type"]; // "direction", "internal", and "patchNames" are option string direction="x"; if (part.found("direction")) { direction=part["direction"]; } bool internal=true; if (part.found("internal")) { internal=readBool(part["internal"]); } wordList patchNames; if (part.found("patchNames")){ patchNames=wordList(part.lookup("patchNames")); } const string &time = runTime.timeName(); Info <<"time " <<time <<"\n"; // for scalar field if(type=="scalar"){ Info << "set " <<field <<endl; // read profile List<quad> profile(part.lookup("profile")); scalarField posX(profile.size()); scalarField posY(profile.size()); scalarField posZ(profile.size()); scalarField fX(profile.size()); forAll(profile,i) { posX[i]=profile[i][0]; posY[i]=profile[i][1]; posZ[i]=profile[i][2]; fX[i]=profile[i][3]; } // read target field (scalar) volScalarField tmpField ( IOobject ( field, time, mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); //read cellCenter of targetMesh volVectorField center=tmpField.mesh().C(); //internalField if(internal){ scalarField &Ifield = tmpField.internalField(); scalarField Icenter(Ifield.size()); scalarField newIField(Ifield.size()); if(direction=="x"){ Icenter = center.internalField().component(vector::X); newIField = interpolateXY(Icenter,posX,fX); } else if(direction=="y"){ Icenter = center.internalField().component(vector::Y); newIField = interpolateXY(Icenter,posY,fX); } else if(direction=="z"){ Icenter = center.internalField().component(vector::Z); newIField = interpolateXY(Icenter,posZ,fX); } Ifield = newIField; } //patch forAll(patchNames,patchNameI) { label patchID=mesh.boundaryMesh().findPatchID(patchNames[patchNameI]); scalarField &Pfield = tmpField.boundaryField()[patchID]; scalarField newPField(Pfield.size()); scalarField Pcenter(Pfield.size()); if(direction=="x"){ Pcenter = center.boundaryField()[patchID].component(vector::X); newPField = interpolateXY(Pcenter,posX,fX); } else if(direction=="y"){ Pcenter = center.boundaryField()[patchID].component(vector::Y); newPField = interpolateXY(Pcenter,posY,fX); } else if(direction=="z"){ Pcenter = center.boundaryField()[patchID].component(vector::Z); newPField = interpolateXY(Pcenter,posZ,fX); } Pfield = newPField; }
void Ball::drawTexture() { b2Vec2 position = ball->GetPosition(); #define fX(x) ((int)(x * (1 << 16))) int square[12] = { fX(-0.5), fX(-0.5), 0, fX(0.5), fX(-0.5), 0, fX(-0.5), fX(0.5), 0, fX(0.5), fX(0.5), 0 }; int texCoords[8] = { 0, fX(1), fX(1), fX(1), 0,0, fX(1),0 }; glPushMatrix(); glTranslatef(position.x, position.y, 0); glScalef(140,140,0); // m_joint->GetJointAngle() float angle = radianToDegrees( ball->GetAngle() ); glRotatef(angle,0.0f,0.0f,1.0f); // LOGI("praktyczna rotacja o: %f \n", angle); glBindTexture( GL_TEXTURE_2D, texture ); glVertexPointer(3, GL_FIXED, 0, square); glTexCoordPointer(2, GL_FIXED, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); }
#include <float.h> #include <assert.h> #include <GLES/gl.h> #include "logger.h" #include "app.h" #include "texture.h" //float to fixed point #define fX(x) ((int)(x * (1 << 16))) #define LOG_TAG "MyShuttleGLSample" static GLint vertices[][3] = { { fX(-1), fX(-1), 0 }, { fX(-1), fX( 1), 0 }, { fX( 1), fX( 1), 0 }, { fX( 1), fX(-1), 0 } }; static int texCoords[8] = { fX(0), fX(1), fX(0), fX(0), fX(1), fX(0), fX(1), fX(1) }; GLubyte indices[] = { 0, 1, 2, 2, 3, 0
void Button::drawTexture() { #define fX(x) ((int)(x * (1 << 16))) int square[12] = { fX(-0.5), fX(-0.5), 0, fX(0.5), fX(-0.5), 0, fX(-0.5), fX(0.5), 0, fX(0.5), fX(0.5), 0 }; int texCoords[8] = { 0, fX(1), fX(1), fX(1), 0,0, fX(1),0 }; glPushMatrix(); if(active) { glBindTexture( GL_TEXTURE_2D, bred ); } else { glBindTexture( GL_TEXTURE_2D, bblue ); } glTranslatef(point[0][0], point[0][1], 0); glScalef(50,50,0); glVertexPointer(3, GL_FIXED, 0, square); glTexCoordPointer(2, GL_FIXED, 0, texCoords); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glPopMatrix(); }