コード例 #1
0
ファイル: W1.cpp プロジェクト: elfion/nanoengineer
int main(int argc, char *argv[])
{
    int a, b, terminator, index;
    double c;
    char *p;

    if (argc < 6)
	goto bad_input;
    a = strtol(argv[1], &p, 10);
    if (*p != '\0') goto bad_input;
    b = strtol(argv[2], &p, 10);
    if (*p != '\0') goto bad_input;
    c = strtod(argv[3], &p);
    if (*p != '\0') goto bad_input;
    terminator = strtol(argv[4], &p, 10);
    if (*p != '\0') goto bad_input;
    index = strtol(argv[5], &p, 10);
    if (*p != '\0') {
    bad_input:
	std::cerr << "BAD INPUT\n";
	return -1;
    }
    W1 w1 = W1(a, b, c, terminator);
    w1.molecule.mmp(std::cout, index);
    return 0;
}
コード例 #2
0
//calculate ES energy between pDNA
FLOAT pdna::pDNA_esenergy(proteinPDB p, matrix Wp, bdna *b) {
  FLOAT E = 0.0;
  FLOAT kap = 0.329*sqrt(ion);
  FLOAT r,x,y,z;
  FLOAT q1, q2;
  matrix W1 = identity(4);

  //translate the positions of the protein
  FLOAT *pex = new FLOAT[p.neatoms];
  FLOAT *pey = new FLOAT[p.neatoms];
  FLOAT *pez = new FLOAT[p.neatoms];
  
 for (int i = 0; i < p.neatoms; i++) {
	pex[i] = Wp(1,4)+p.ex[i]*Wp(1,1)+p.ey[i]*Wp(1,2)+p.ez[i]*Wp(1,3);
	pey[i] = Wp(2,4)+p.ex[i]*Wp(2,1)+p.ey[i]*Wp(2,2)+p.ez[i]*Wp(2,3);
	pez[i] = Wp(3,4)+p.ex[i]*Wp(3,1)+p.ey[i]*Wp(3,2)+p.ez[i]*Wp(3,3);
  }
  
  for (int i = 0; i < b->nsteps-1; i++) {
 
    FLOAT X1 = W1(1,4);
    FLOAT Y1 = W1(2,4);
    FLOAT Z1 = W1(3,4);

    q1 = -0.25; //DNA screened charge

    W1 = W1 * calculateW(v[i]);

	for (int j = 0; j < p.neatoms; j++) {
      x = X1-pex[j];
      y = Y1-pey[j];
      z = Z1-pez[j]; 
	  q2 = p.e[j];
      r = sqrt(x*x+y*y+z*z);
      E += q1*q2*exp(-kap*r)/r;
    }
  }
  E *= coeff;

  return E;
  delete [] pex; delete [] pey; delete [] pez;
  
}
コード例 #3
0
ファイル: Wm5MeshCurvature.cpp プロジェクト: fishxz/omni-bot
MeshCurvature<Real>::MeshCurvature (int numVertices,
    const Vector3<Real>* vertices, int numTriangles, const int* indices)
{
    mNumVertices = numVertices;
    mVertices = vertices;
    mNumTriangles = numTriangles;
    mIndices = indices;

    // Compute normal vectors.
    mNormals = new1<Vector3<Real> >(mNumVertices);
    memset(mNormals, 0, mNumVertices*sizeof(Vector3<Real>));
    int i, v0, v1, v2;
    for (i = 0; i < mNumTriangles; ++i)
    {
        // Get vertex indices.
        v0 = *indices++;
        v1 = *indices++;
        v2 = *indices++;

        // Compute the normal (length provides a weighted sum).
        Vector3<Real> edge1 = mVertices[v1] - mVertices[v0];
        Vector3<Real> edge2 = mVertices[v2] - mVertices[v0];
        Vector3<Real> normal = edge1.Cross(edge2);

        mNormals[v0] += normal;
        mNormals[v1] += normal;
        mNormals[v2] += normal;
    }
    for (i = 0; i < mNumVertices; ++i)
    {
        mNormals[i].Normalize();
    }

    // Compute the matrix of normal derivatives.
    Matrix3<Real>* DNormal = new1<Matrix3<Real> >(mNumVertices);
    Matrix3<Real>* WWTrn = new1<Matrix3<Real> >(mNumVertices);
    Matrix3<Real>* DWTrn = new1<Matrix3<Real> >(mNumVertices);
    bool* DWTrnZero = new1<bool>(mNumVertices);
    memset(WWTrn, 0, mNumVertices*sizeof(Matrix3<Real>));
    memset(DWTrn, 0, mNumVertices*sizeof(Matrix3<Real>));
    memset(DWTrnZero, 0, mNumVertices*sizeof(bool));

    int row, col;
    indices = mIndices;
    for (i = 0; i < mNumTriangles; ++i)
    {
        // Get vertex indices.
        int V[3];
        V[0] = *indices++;
        V[1] = *indices++;
        V[2] = *indices++;

        for (int j = 0; j < 3; j++)
        {
            v0 = V[j];
            v1 = V[(j+1)%3];
            v2 = V[(j+2)%3];

            // Compute edge from V0 to V1, project to tangent plane of vertex,
            // and compute difference of adjacent normals.
            Vector3<Real> E = mVertices[v1] - mVertices[v0];
            Vector3<Real> W = E - (E.Dot(mNormals[v0]))*mNormals[v0];
            Vector3<Real> D = mNormals[v1] - mNormals[v0];
            for (row = 0; row < 3; ++row)
            {
                for (col = 0; col < 3; ++col)
                {
                    WWTrn[v0][row][col] += W[row]*W[col];
                    DWTrn[v0][row][col] += D[row]*W[col];
                }
            }

            // Compute edge from V0 to V2, project to tangent plane of vertex,
            // and compute difference of adjacent normals.
            E = mVertices[v2] - mVertices[v0];
            W = E - (E.Dot(mNormals[v0]))*mNormals[v0];
            D = mNormals[v2] - mNormals[v0];
            for (row = 0; row < 3; ++row)
            {
                for (col = 0; col < 3; ++col)
                {
                    WWTrn[v0][row][col] += W[row]*W[col];
                    DWTrn[v0][row][col] += D[row]*W[col];
                }
            }
        }
    }

    // Add in N*N^T to W*W^T for numerical stability.  In theory 0*0^T gets
    // added to D*W^T, but of course no update is needed in the
    // implementation.  Compute the matrix of normal derivatives.
    for (i = 0; i < mNumVertices; ++i)
    {
        for (row = 0; row < 3; ++row)
        {
            for (col = 0; col < 3; ++col)
            {
                WWTrn[i][row][col] = ((Real)0.5)*WWTrn[i][row][col] +
                    mNormals[i][row]*mNormals[i][col];
                DWTrn[i][row][col] *= (Real)0.5;
            }
        }

        // Compute the max-abs entry of D*W^T.  If this entry is (nearly)
        // zero, flag the DNormal matrix as singular.
        Real maxAbs = (Real)0;
        for (row = 0; row < 3; ++row)
        {
            for (col = 0; col < 3; ++col)
            {
                Real absEntry = Math<Real>::FAbs(DWTrn[i][row][col]);
                if (absEntry > maxAbs)
                {
                    maxAbs = absEntry;
                }
            }
        }
        if (maxAbs < (Real)1e-07)
        {
            DWTrnZero[i] = true;
        }

        DNormal[i] = DWTrn[i]*WWTrn[i].Inverse();
    }

    delete1(WWTrn);
    delete1(DWTrn);

    // If N is a unit-length normal at a vertex, let U and V be unit-length
    // tangents so that {U, V, N} is an orthonormal set.  Define the matrix
    // J = [U | V], a 3-by-2 matrix whose columns are U and V.  Define J^T
    // to be the transpose of J, a 2-by-3 matrix.  Let dN/dX denote the
    // matrix of first-order derivatives of the normal vector field.  The
    // shape matrix is
    //   S = (J^T * J)^{-1} * J^T * dN/dX * J = J^T * dN/dX * J
    // where the superscript of -1 denotes the inverse.  (The formula allows
    // for J built from non-perpendicular vectors.) The matrix S is 2-by-2.
    // The principal curvatures are the eigenvalues of S.  If k is a principal
    // curvature and W is the 2-by-1 eigenvector corresponding to it, then
    // S*W = k*W (by definition).  The corresponding 3-by-1 tangent vector at
    // the vertex is called the principal direction for k, and is J*W.
    mMinCurvatures = new1<Real>(mNumVertices);
    mMaxCurvatures = new1<Real>(mNumVertices);
    mMinDirections = new1<Vector3<Real> >(mNumVertices);
    mMaxDirections = new1<Vector3<Real> >(mNumVertices);
    for (i = 0; i < mNumVertices; ++i)
    {
        // Compute U and V given N.
        Vector3<Real> U, V;
        Vector3<Real>::GenerateComplementBasis(U, V, mNormals[i]);

        if (DWTrnZero[i])
        {
            // At a locally planar point.
            mMinCurvatures[i] = (Real)0;
            mMaxCurvatures[i] = (Real)0;
            mMinDirections[i] = U;
            mMaxDirections[i] = V;
            continue;
        }

        // Compute S = J^T * dN/dX * J.  In theory S is symmetric, but
        // because we have estimated dN/dX, we must slightly adjust our
        // calculations to make sure S is symmetric.
        Real s01 = U.Dot(DNormal[i]*V);
        Real s10 = V.Dot(DNormal[i]*U);
        Real sAvr = ((Real)0.5)*(s01 + s10);
        Matrix2<Real> S
        (
            U.Dot(DNormal[i]*U), sAvr,
            sAvr, V.Dot(DNormal[i]*V)
        );

        // Compute the eigenvalues of S (min and max curvatures).
        Real trace = S[0][0] + S[1][1];
        Real det = S[0][0]*S[1][1] - S[0][1]*S[1][0];
        Real discr = trace*trace - ((Real)4.0)*det;
        Real rootDiscr = Math<Real>::Sqrt(Math<Real>::FAbs(discr));
        mMinCurvatures[i] = ((Real)0.5)*(trace - rootDiscr);
        mMaxCurvatures[i] = ((Real)0.5)*(trace + rootDiscr);

        // Compute the eigenvectors of S.
        Vector2<Real> W0(S[0][1], mMinCurvatures[i] - S[0][0]);
        Vector2<Real> W1(mMinCurvatures[i] - S[1][1], S[1][0]);
        if (W0.SquaredLength() >= W1.SquaredLength())
        {
            W0.Normalize();
            mMinDirections[i] = W0.X()*U + W0.Y()*V;
        }
        else
        {
            W1.Normalize();
            mMinDirections[i] = W1.X()*U + W1.Y()*V;
        }

        W0 = Vector2<Real>(S[0][1], mMaxCurvatures[i] - S[0][0]);
        W1 = Vector2<Real>(mMaxCurvatures[i] - S[1][1], S[1][0]);
        if (W0.SquaredLength() >= W1.SquaredLength())
        {
            W0.Normalize();
            mMaxDirections[i] = W0.X()*U + W0.Y()*V;
        }
        else
        {
            W1.Normalize();
            mMaxDirections[i] = W1.X()*U + W1.Y()*V;
        }
    }

    delete1(DWTrnZero);
    delete1(DNormal);
}
コード例 #4
0
/// Adaptive Weights disparity computation.
///
/// The dissimilarity is computed putting adaptive weights on the raw cost.
/// \param im1,im2 the two color images
/// \param dMin,dMax disparity range
/// \param param raw cost computation parameters
/// \param disp1 output disparity map from image 1 to image 2
/// \param disp2 output disparity map from image 2 to image 1
void disparityAW(Image im1, Image im2,
                 int dMin, int dMax, const ParamDisparity& param,
                 Image& disp1, Image& disp2) {
    const int width=im1.width(), height=im1.height();
    const int r = param.radius;
#ifdef COMB_LEFT // Disparity range
    const int nd = 1; // Do not compute useless weights in target image
#else
    const int nd = dMax-dMin+1;
#endif

    // Tabulated proximity weights (color distance)
    const int maxL1 = im1.channels()*255; // Maximum L1 distance between colors
    float* distC = new float[maxL1+1];
    float e2=exp(-1/(im1.channels()*param.gammaCol));
    distC[0]=1.0f;
    for(int x=1; x<=maxL1; x++)
        distC[x] = e2*distC[x-1]; // distC[x] = exp(-x/(c*gamma))

    // Tabulated proximity weights (spatial distance)
    const int dim=2*r+1; // window dimension
    float *distP = new float[dim*dim], *d=distP;
    for(int y=-r; y<=r; y++)
    for(int x=-r; x<=r; x++)
        *d++ = exp(-2.0f*sqrt((float)(x*x+y*y))/param.gammaPos);

    Image* cost = costVolume(im1, im2, dMin, dMax, param);

    // Images of dissimilarity 1->2 and 2->1
    Image E1(width,height), E2(width,height);
    std::fill_n(&E1(0,0), width*height, std::numeric_limits<float>::max());
    std::fill_n(&E2(0,0), width*height, std::numeric_limits<float>::max());

#ifdef _OPENMP
#pragma omp parallel for
#endif
    for(int y=0; y<height; y++) {
        // Weight window in reference image
        Image W1(dim,dim);
        // Weight windows in target image for each disparity (useless for
        // COMB_LEFT, but better to have readable code than multiplying #ifdef)
        Image* weights2 = new Image[nd];
        for(int d=0; d<nd; d++) {
            weights2[d] = Image(dim,dim);
            if(d+1<nd) // Support for dMax computed later
                support(im2, d,y, r, distC, weights2[d]);
        }

        for(int x=0; x<width; x++) {
            // Reference window weights
            support(im1, x,y, r, distC, W1);
#ifndef COMB_LEFT // Weight window at disparity dMax in target image
            support(im2, x+dMax,y, r, distC, weights2[(x+dMax-dMin)%nd]);
#endif
            for(int d=dMin; d<=dMax; d++) {
                if(0<=x+d && x+d<width) {
                    const Image& e = cost[d-dMin]; // Raw cost for disp. d
                    const Image& W2 = weights2[(x+d-dMin)%nd];
                    float E = costCombined(x, x+d, y, r, W1, W2, distP, e);
                    if(E1(x,y) > E) {
                        E1(x,y) = E;
                        disp1(x,y) = static_cast<float>(d);
                    }
                    if(E2(x+d,y) > E) {
                        E2(x+d,y) = E;
                        disp2(x+d,y)= -static_cast<float>(d);
                    }
                }
            }
        }
        delete [] weights2;
    }
    delete [] cost;
    delete [] distC;
    delete [] distP;
}
コード例 #5
0
ファイル: SharedIC-arm64.cpp プロジェクト: luke-chang/gecko-1
bool
ICBinaryArith_Int32::Compiler::generateStubCode(MacroAssembler& masm)
{
    // Guard that R0 is an integer and R1 is an integer.
    Label failure;
    masm.branchTestInt32(Assembler::NotEqual, R0, &failure);
    masm.branchTestInt32(Assembler::NotEqual, R1, &failure);

    // Add R0 and R1. Don't need to explicitly unbox, just use R2.
    Register Rscratch = R2_;
    ARMRegister Wscratch = ARMRegister(Rscratch, 32);
#ifdef MERGE
    // DIV and MOD need an extra non-volatile ValueOperand to hold R0.
    AllocatableGeneralRegisterSet savedRegs(availableGeneralRegs(2));
    savedRegs.set() = GeneralRegisterSet::Intersect(GeneralRegisterSet::NonVolatile(), savedRegs);
#endif
    // get some more ARM-y names for the registers
    ARMRegister W0(R0_, 32);
    ARMRegister X0(R0_, 64);
    ARMRegister W1(R1_, 32);
    ARMRegister X1(R1_, 64);
    ARMRegister WTemp(ExtractTemp0, 32);
    ARMRegister XTemp(ExtractTemp0, 64);
    Label maybeNegZero, revertRegister;
    switch(op_) {
      case JSOP_ADD:
        masm.Adds(WTemp, W0, Operand(W1));

        // Just jump to failure on overflow. R0 and R1 are preserved, so we can
        // just jump to the next stub.
        masm.j(Assembler::Overflow, &failure);

        // Box the result and return. We know R0 already contains the
        // integer tag, so we just need to move the payload into place.
        masm.movePayload(ExtractTemp0, R0_);
        break;

      case JSOP_SUB:
        masm.Subs(WTemp, W0, Operand(W1));
        masm.j(Assembler::Overflow, &failure);
        masm.movePayload(ExtractTemp0, R0_);
        break;

      case JSOP_MUL:
        masm.mul32(R0.valueReg(), R1.valueReg(), Rscratch, &failure, &maybeNegZero);
        masm.movePayload(Rscratch, R0_);
        break;

      case JSOP_DIV:
      case JSOP_MOD: {

        // Check for INT_MIN / -1, it results in a double.
        Label check2;
        masm.Cmp(W0, Operand(INT_MIN));
        masm.B(&check2, Assembler::NotEqual);
        masm.Cmp(W1, Operand(-1));
        masm.j(Assembler::Equal, &failure);
        masm.bind(&check2);
        Label no_fail;
        // Check for both division by zero and 0 / X with X < 0 (results in -0).
        masm.Cmp(W1, Operand(0));
        // If x > 0, then it can't be bad.
        masm.B(&no_fail, Assembler::GreaterThan);
        // if x == 0, then ignore any comparison, and force
        // it to fail, if x < 0 (the only other case)
        // then do the comparison, and fail if y == 0
        masm.Ccmp(W0, Operand(0), vixl::ZFlag, Assembler::NotEqual);
        masm.B(&failure, Assembler::Equal);
        masm.bind(&no_fail);
        masm.Sdiv(Wscratch, W0, W1);
        // Start calculating the remainder, x - (x / y) * y.
        masm.mul(WTemp, W1, Wscratch);
        if (op_ == JSOP_DIV) {
            // Result is a double if the remainder != 0, which happens
            // when (x/y)*y != x.
            masm.branch32(Assembler::NotEqual, R0.valueReg(), ExtractTemp0, &revertRegister);
            masm.movePayload(Rscratch, R0_);
        } else {
            // Calculate the actual mod. Set the condition code, so we can see if it is non-zero.
            masm.Subs(WTemp, W0, WTemp);

            // If X % Y == 0 and X < 0, the result is -0.
            masm.Ccmp(W0, Operand(0), vixl::NoFlag, Assembler::Equal);
            masm.branch(Assembler::LessThan, &revertRegister);
            masm.movePayload(ExtractTemp0, R0_);
        }
        break;
      }
        // ORR, EOR, AND can trivially be coerced int
        // working without affecting the tag of the dest..
      case JSOP_BITOR:
        masm.Orr(X0, X0, Operand(X1));
        break;
      case JSOP_BITXOR:
        masm.Eor(X0, X0, Operand(W1, vixl::UXTW));
        break;
      case JSOP_BITAND:
        masm.And(X0, X0, Operand(X1));
        break;
        // LSH, RSH and URSH can not.
      case JSOP_LSH:
        // ARM will happily try to shift by more than 0x1f.
        masm.Lsl(Wscratch, W0, W1);
        masm.movePayload(Rscratch, R0.valueReg());
        break;
      case JSOP_RSH:
        masm.Asr(Wscratch, W0, W1);
        masm.movePayload(Rscratch, R0.valueReg());
        break;
      case JSOP_URSH:
        masm.Lsr(Wscratch, W0, W1);
        if (allowDouble_) {
            Label toUint;
            // Testing for negative is equivalent to testing bit 31
            masm.Tbnz(Wscratch, 31, &toUint);
            // Move result and box for return.
            masm.movePayload(Rscratch, R0_);
            EmitReturnFromIC(masm);

            masm.bind(&toUint);
            masm.convertUInt32ToDouble(Rscratch, ScratchDoubleReg);
            masm.boxDouble(ScratchDoubleReg, R0, ScratchDoubleReg);
        } else {
            // Testing for negative is equivalent to testing bit 31
            masm.Tbnz(Wscratch, 31, &failure);
            // Move result for return.
            masm.movePayload(Rscratch, R0_);
        }
        break;
      default:
        MOZ_CRASH("Unhandled op for BinaryArith_Int32.");
    }

    EmitReturnFromIC(masm);

    switch (op_) {
      case JSOP_MUL:
        masm.bind(&maybeNegZero);

        // Result is -0 if exactly one of lhs or rhs is negative.
        masm.Cmn(W0, W1);
        masm.j(Assembler::Signed, &failure);

        // Result is +0, so use the zero register.
        masm.movePayload(rzr, R0_);
        EmitReturnFromIC(masm);
        break;
      case JSOP_DIV:
      case JSOP_MOD:
        masm.bind(&revertRegister);
        break;
      default:
        break;
    }

    // Failure case - jump to next stub.
    masm.bind(&failure);
    EmitStubGuardFailure(masm);

    return true;
}
コード例 #6
0
  virtual void paintGL()
  {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    int xOffset = -70;
    int yOffset = -50;
    int zoom = -200;
    glTranslatef(xOffset, yOffset, zoom);

    glColor3f(0.0f, 0.0f, 0.0f);
    glLineWidth(5.0);

    float scale = 1.0;

    if(showFilters)
    {
      Eigen::MatrixXd W1 = sae.getInputWeights();
      Eigen::MatrixXd W2 = sae.getOutputWeights();
      double mi = sae.getInputWeights().minCoeff();
      double ma = sae.getInputWeights().maxCoeff();
      double range = ma - mi;
      for(int row = 0, filter = 0; row < neuronRows; row++)
      {
        for(int col = 0; col < neuronCols; col++, filter++)
        {
          glBegin(GL_QUADS);
          for(int yIdx = 0; yIdx < 28; yIdx++)
          {
            for(int xIdx = 0; xIdx < 28; xIdx++)
            {
              int h = (filter + offset) / 2;
              bool in = (filter + offset) % 2 == 0;
              int idx = yIdx * 28 + xIdx;
              float c = ((in ? W1(h, idx) : W2(idx, h)) - mi) / range;
              float x = xIdx * scale + col * 29.0f * scale - 30.0f;
              float y = (28.0f - yIdx) * scale - row * scale * 29.0f + 90.0f;
              glColor3f(c, c, c);
              glVertex2f(x, y);
              glVertex2f(x + scale, y);
              glVertex2f(x + scale, y + scale);
              glVertex2f(x, y + scale);
            }
          }
          glEnd();
        }
      }
    }
    else
    {
      for(int row = 0; row < neuronRows; row++)
      {
        for(int col = 0; col < neuronCols; col++)
        {
          Eigen::VectorXd out = sae.reconstruct(dataSet.getInstance(offset + row*neuronCols+col));
          glBegin(GL_QUADS);
          for(int yIdx = 0; yIdx < 28; yIdx++)
          {
            for(int xIdx = 0; xIdx < 28; xIdx++)
            {
              int idx = yIdx * 28 + xIdx;
              float c = out(idx);
              float x = xIdx * scale + col * 29.0f * scale - 30.0f;
              float y = (28.0f - yIdx) * scale - row * scale * 29.0f + 90.0f;
              glColor3f(c, c, c);
              glVertex2f(x, y);
              glVertex2f(x + scale, y);
              glVertex2f(x + scale, y + scale);
              glVertex2f(x, y + scale);
            }
          }
          glEnd();
        }
      }
    }

    glColor3f(0.0f, 0.0f, 0.0f);
    renderText(10, 35, "MNIST data set", QFont("Helvetica", 20));

    glFlush();
  }