Пример #1
0
int sapporo::set_j_particle(int    address,
                            int    id,
                            double tj, double dtj,
                            double mass,
                            double k18[3],       double j6[3],
                            double a2[3],        double v[3],
                            double x[3],         double snp[3],
                            double crk[3],       double eps) {

  #ifdef DEBUG_PRINT
    cerr << "set_j_particle (Addr: " << address << "  Id: " << id << " )\n";
  #endif

  //Prevent unused compiler warning
  k18 = k18;
    
  predJOnHost  = false; //Reset the buffers on the device since they can be modified
  nj_updated   = true;  //There are particles that are updated

  //Check if the address does not fall outside the allocated memory range
  //if it falls outside that range increase j-memory by 10%
    
  if (address >= nj_max) {
    fprintf(stderr, "Increasing nj_max! Nj_max was: %d  to be stored address: %d \n",
            nj_max, address);
    increase_jMemory();

    //Extra check, if we are still outside nj_max, we quit since particles are not
    //nicely send in order
    if (address >= nj_max) {
      fprintf(stderr, "Increasing nj_max was not enough! Send particles in order to the library! Exit\n");
      exit(-1);
    }
  }

  //Memory has been allocated, now we can store the particles
  //First calculate on which device this particle has to be stored
  //and on which physical address on that device. Note that the particles
  //are distributed to the different devices in a round-robin way (based on the addres)
  int dev           = address % nCUDAdevices;
  int devAddr       = address / nCUDAdevices;
  int storeLoc      = jCopyInformation[dev].count;

  //Store this information, incase particles get overwritten
  map<int, int4>::iterator iterator = mappingFromIndexToDevIndex.find(address);
  map<int, int4>::iterator end      = mappingFromIndexToDevIndex.end();


  if(iterator != end)
  {
    //Particle with this address has been set before, retrieve previous
    //calculated indices and overwrite them with the new info
    int4 addrInfo = (*iterator).second;
    dev           = addrInfo.x;
    storeLoc      = addrInfo.y;
    devAddr       = addrInfo.z;
  }
  else
  {
    //New particle not set before, save address info and increase particles
    //on that specific device by one
    mappingFromIndexToDevIndex[address] = make_int4(dev, storeLoc, devAddr, -1);
    jCopyInformation[dev].count++;
  }


  deviceList[dev]->pos_j_temp[storeLoc] = make_double4(x[0], x[1], x[2], mass);
  deviceList[dev]->address_j[storeLoc]  = devAddr;
  
  if(integrationOrder > GRAPE5)
  {
    deviceList[dev]->t_j_temp[storeLoc]          = make_double2(tj, dtj);
    deviceList[dev]->vel_j_temp[storeLoc]        = make_double4(v[0], v[1], v[2], eps);
    deviceList[dev]->acc_j_temp[storeLoc]        = make_double4(a2[0], a2[1], a2[2], 0.0);
    deviceList[dev]->jrk_j_temp[storeLoc]        = make_double4(j6[0], j6[1], j6[2], 0.0);
    deviceList[dev]->id_j_temp[storeLoc]         = id;
    //For 6th and 8 order we need more parameters
    if(integrationOrder > FOURTH)
    {
      deviceList[dev]->snp_j_temp[storeLoc]        = make_double4(snp[0], snp[1], snp[2], 0.0);
      deviceList[dev]->crk_j_temp[storeLoc]        = make_double4(crk[0], crk[1], crk[2], 0.0);
    }
  }
  

  #ifdef CPU_SUPPORT
    //Put the new j particles directly in the correct location on the host side.
    deviceList[dev]->pos_j[devAddr] = make_double4(x[0], x[1], x[2], mass);
    
    if(integrationOrder > GRAPE5)
    {
      deviceList[dev]->t_j[devAddr]          = make_double2(tj, dtj);
      deviceList[dev]->vel_j[devAddr]        = make_double4(v[0], v[1], v[2], eps);
      deviceList[dev]->acc_j[devAddr]        = make_double4(a2[0], a2[1], a2[2], 0.0);
      deviceList[dev]->jrk_j[devAddr]        = make_double4(j6[0], j6[1], j6[2], 0.0);
      deviceList[dev]->id_j[devAddr]         = id;
      //For 6th and 8 order we need more parameters
      if(integrationOrder > FOURTH)
      {
        deviceList[dev]->snp_j[devAddr]        = make_double4(snp[0], snp[1], snp[2], 0.0);
        deviceList[dev]->crk_j[devAddr]        = make_double4(crk[0], crk[1], crk[2], 0.0);
      }
    }  
  #endif


  #ifdef DEBUG_PRINT
    if(integrationOrder == GRAPE5)
    {
      fprintf(stderr, "Setj ad: %d\tid: %d storeLoc: %d \tpos: %f %f %f m: %f \n", address, id, storeLoc, x[0],x[1],x[2], mass);
    }
    else
    {
      fprintf(stderr, "Setj ad: %d\tid: %d storeLoc: %d \tpos: %f %f %f\t mass: %f \tvel: %f %f %f", address, id, storeLoc, x[0],x[1],x[2],mass, v[0],v[1],v[2]);
      fprintf(stderr, "\tacc: %f %f %f \n", a2[0],a2[1],a2[2]);
      if(integrationOrder > FOURTH)
      {
        fprintf(stderr, "\tsnp: %f %f %f ", snp[0],snp[1],snp[2]);
        fprintf(stderr, "\tcrk: %f %f %f \n", crk[0],crk[1],crk[2]);
      }
    }
  #endif

  return 0;
};
Пример #2
0
void testOverlap(vtkPolyData *data, uint y, uint z, bool returnAllResults, uint res)
{
    double3* vertices;
    uint3* indices;
    double3* normals;
    double3 minVertex;
    double3 maxVertex;
    double voxelDistance;
    uint3 resolution;
    bool testState;

    // Get triangles with indices (polys) and the vertices (points).
    vtkCellArray* polys = data->GetPolys();
    vtkPoints* points = data->GetPoints();

    uint numberOfVertices = (uint)points->GetNumberOfPoints();
    cout << numberOfVertices << " vertices found.\n";
    uint numberOfPolygons = (uint)polys->GetNumberOfCells();
    cout << numberOfPolygons << " polygons found.\n";

    // Allocate vertex and index arrays in main memory.
    vertices = new double3[numberOfVertices];
    indices = new uint3[numberOfPolygons];
    normals = new double3[numberOfPolygons];

    // Calculate the bounding box of the input model.
    double vert[3];
    points->GetPoint(0, vert);

    minVertex.x = vert[0];
    maxVertex.x = vert[0];
    minVertex.y = vert[1];
    maxVertex.y = vert[1];
    minVertex.z = vert[2];
    maxVertex.z = vert[2];

    for (vtkIdType i = 1; i < points->GetNumberOfPoints(); i++)
    {
        points->GetPoint(i, vert);
        // Determine minimum and maximum coordinates.

        if (vert[0] > maxVertex.x)
            maxVertex.x = vert[0];
        if (vert[1] > maxVertex.y)
            maxVertex.y = vert[1];
        if (vert[2] > maxVertex.z)
            maxVertex.z = vert[2];

        if (vert[0] < minVertex.x)
            minVertex.x = vert[0];
        if (vert[1] < minVertex.y)
            minVertex.y = vert[1];
        if (vert[2] < minVertex.z)
            minVertex.z = vert[2];
    }

    cout << "Minimum corner: " << minVertex.x << ", " << minVertex.y << ", " << minVertex.z << "\n";
    cout << "Maximum corner: " << maxVertex.x << ", " << maxVertex.y << ", " << maxVertex.z << "\n";

    // Copy vertex data to the device.
    for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
    {
        points->GetPoint(i, vert);

        vertices[i] = make_double3(vert[0], vert[1], vert[2]); // - minVertex;
    }

    // Copy index data to the array.
    vtkIdList* idlist = vtkIdList::New();
    uint currentPoly = 0;
    polys->InitTraversal();
    while(polys->GetNextCell(idlist))
    {
        uint index[3] = { 0, 0, 0 };
        for (vtkIdType i = 0; i < idlist->GetNumberOfIds(); i++)
        {
            if (i > 2)
            {
                cout << "Too many indices! Only triangle-meshes are supported.";
                return;
            }

            index[i] = idlist->GetId(i);
        }

        indices[currentPoly] = make_uint3(index[0], index[1], index[2]);

        // Calculate the surface normal of the current polygon.
        double3 U = make_double3(vertices[indices[currentPoly].x].x - vertices[indices[currentPoly].z].x,
                               vertices[indices[currentPoly].x].y - vertices[indices[currentPoly].z].y, 
                               vertices[indices[currentPoly].x].z - vertices[indices[currentPoly].z].z);
        double3 V = make_double3(vertices[indices[currentPoly].y].x - vertices[indices[currentPoly].x].x,
                               vertices[indices[currentPoly].y].y - vertices[indices[currentPoly].x].y, 
                               vertices[indices[currentPoly].y].z - vertices[indices[currentPoly].x].z);

        normals[currentPoly] = normalize(cross(U, V));

        currentPoly++;
    }

    double3 diffVertex = maxVertex - minVertex;

    if (diffVertex.x > diffVertex.y)
    {
        if (diffVertex.x > diffVertex.z)
        {
            voxelDistance = diffVertex.x / double(res - 1);
            resolution.x = res;
            resolution.y = uint(diffVertex.y / voxelDistance) + 1;
            resolution.z = uint(diffVertex.z / voxelDistance) + 1;
        }
        else
        {
            voxelDistance = diffVertex.z / double(res - 1);
            resolution.x = uint(diffVertex.x / voxelDistance) + 1;
            resolution.y = uint(diffVertex.y / voxelDistance) + 1;
            resolution.z = res;
        }
    }
    else
    {
        if (diffVertex.y > diffVertex.z)
        {
            voxelDistance = diffVertex.y / double(res - 1);
            resolution.x = uint(diffVertex.x / voxelDistance) + 1;
            resolution.y = res;
            resolution.z = uint(diffVertex.z / voxelDistance) + 1;
        }
        else
        {
            voxelDistance = diffVertex.z / double(res - 1);
            resolution.x = uint(diffVertex.x / voxelDistance) + 1;
            resolution.y = uint(diffVertex.y / voxelDistance) + 1;
            resolution.z = res;
        }
    }

    if (resolution.x % 32 != 0)
        resolution.x += 32 - (resolution.x % 32);
    if (resolution.y % 16 != 0)
        resolution.y += 16 - (resolution.y % 16);
    if (resolution.z % 16 != 0)
        resolution.z += 16 - (resolution.z % 16);

    cout << "Voxel width: " << voxelDistance << "\n";
    cout << "Resolution: " << resolution.x << " X " << resolution.y << " X " << resolution.z << "\n";
    cout << "YZ-coordinates: (" << y << ", " << z << ")\n\n";

    uint intersectionCount = 0;
    std::vector<uint> intersections = std::vector<uint>();

    // Shoot a ray from the voxel coordinates, and test for intersection against every triangle.
    for (uint i = 0; i < numberOfPolygons; i++)
    {
        double2 vertexProjections[3];
        double2 edgeNormals[3];
        double distanceToEdge[3];
        double testResult[3];

        if (normals[i].x == 0.0)
            continue;

        vertexProjections[0] = make_double2(vertices[indices[i].x].y, vertices[indices[i].x].z);
        vertexProjections[1] = make_double2(vertices[indices[i].y].y, vertices[indices[i].y].z);
        vertexProjections[2] = make_double2(vertices[indices[i].z].y, vertices[indices[i].z].z);

        double2 p = make_double2(minVertex.y + double(y) * voxelDistance, minVertex.z + double(z) * voxelDistance);

        double2 vMin = vertexProjections[0];
        double2 vMax = vertexProjections[0];

        if (vertexProjections[1].x < vMin.x)
            vMin.x = vertexProjections[1].x;
        if (vertexProjections[1].y < vMin.y)
            vMin.y = vertexProjections[1].y;
        if (vertexProjections[2].x < vMin.x)
            vMin.x = vertexProjections[2].x;
        if (vertexProjections[2].y < vMin.y)
            vMin.y = vertexProjections[2].y;

        if (vertexProjections[1].x > vMax.x)
            vMax.x = vertexProjections[1].x;
        if (vertexProjections[1].y > vMax.y)
            vMax.y = vertexProjections[1].y;
        if (vertexProjections[2].x > vMax.x)
            vMax.x = vertexProjections[2].x;
        if (vertexProjections[2].y > vMax.y)
            vMax.y = vertexProjections[2].y;

        if ((p.x < vMin.x) || (p.y < vMin.y) || (p.x > vMax.x) || (p.y > vMax.y))
            continue;

        for (uint j = 0; j < 3; j++)
        {
            double2 edge = make_double2(vertexProjections[(j+1)%3].x - vertexProjections[j].x, 
                                        vertexProjections[(j+1)%3].y - vertexProjections[j].y);
            if (normals[i].x >= 0.0)
                edgeNormals[j] = normalize(make_double2(-edge.y, edge.x));
            else
                edgeNormals[j] = normalize(make_double2(edge.y, -edge.x));

            distanceToEdge[j] = edgeNormals[j].x * vertexProjections[j].x + edgeNormals[j].y * vertexProjections[j].y;
            distanceToEdge[j] *= -1.0;

            testResult[j] = distanceToEdge[j] + edgeNormals[j].x * p.x + edgeNormals[j].y * p.y;

            if ((edgeNormals[j].x > 0.0) || ((edgeNormals[j].x == 0) && (edgeNormals[j].y < 0.0)))
                testResult[j] += DBL_MIN;
            
        }

        if ((testResult[0] > 0.0) && (testResult[1] > 0.0) && (testResult[2] > 0.0))
            testState = true;
        else
            testState = false;

        if (testState || returnAllResults)
        {
            if (testState)
            {
                intersectionCount++;
                cout << "Intersection nr. " << intersectionCount << " found with triangle " << i << "\n";
            }
            else
                cout << "No intersection found with triangle " << i << "\n";
            cout << "Vertex 1      : (" << vertexProjections[0].x << ", " << vertexProjections[0].y << ")\n";
            cout << "Vertex 2      : (" << vertexProjections[1].x << ", " << vertexProjections[1].y << ")\n";
            cout << "Vertex 3      : (" << vertexProjections[2].x << ", " << vertexProjections[2].y << ")\n";
            cout << "Normal        : (" << normals[i].x << ", " << normals[i].y << ", " << normals[i].z << ")\n";
            cout << "Edge normal 1 : (" << edgeNormals[0].x << ", " << edgeNormals[0].y << ")\n";
            cout << "Edge normal 2 : (" << edgeNormals[1].x << ", " << edgeNormals[1].y << ")\n";
            cout << "Edge normal 3 : (" << edgeNormals[2].x << ", " << edgeNormals[2].y << ")\n";
            cout << "Distances     : " << distanceToEdge[0] << ", " << distanceToEdge[1] << ", " << distanceToEdge[2] << "\n";
            cout << "Test results  : " << testResult[0] << ", " << testResult[1] << ", " << testResult[2] << "\n";
            cout << "P             : " << p.x << ", " << p.y << "\n";
            cout << (testState ? "Intersection found" : "No intersection") << " at X: ";

            double3 v = vertices[indices[i].x];
            double3 n = normals[i];

            double3 A = make_double3(v.x - minVertex.x, v.y - p.x, v.z - p.y);
            double B = dot(A, n);
            double px = B / n.x;

            uint voxelX = ceilf(px / voxelDistance);

            cout << voxelX << "\n\n";

            if (testState)
                intersections.push_back(voxelX);
        }
    }

    uint voxels[UTIL_RES / 32];

    for (uint i = 0; i < UTIL_RES / 32; i++)
        voxels[i] = 0;

    if (intersectionCount > 0)
    {
        for (std::vector<unsigned int>::iterator it = intersections.begin(); it < intersections.end(); it++)
        {
            uint intersection = *it;

            uint relevantInteger = intersection / 32;
            uint relevantBit = intersection % 32;

            uint bitmask = UINT_MAX >> relevantBit;
            voxels[relevantInteger] ^= bitmask;

            for (uint i = relevantInteger + 1; i < UTIL_RES / 32; i++)
                voxels[i] ^= UINT_MAX;
        }

        cout << "The voxelization for Y: " << y << ", Z: " << z << "\n\n";
        char hexRep[12];
        for (uint i = 0; i < UTIL_RES / 32; i++)
        {
            sprintf( hexRep, "%X", voxels[i] );
            //sprintf_s(hexRep, 12*sizeof(char), "%X", voxels[i]);
            cout << hexRep << " ";
        }

        cout << "\n\n";
    }
    else if (returnAllResults)
Пример #3
0
void parse_config_xml(Parameters &params, const std::string &fname)
{
    static bool initxml = false;
    if(!initxml)
    {
        xmlInitParser();
        xmlXPathInit();
        atexit(xmlCleanupParser);

        initxml = true;
    }

    xmlDoc *doc = NULL;
    xmlXPathContext *ctx = NULL;

    try
    {
        doc = xmlParseFile(fname.c_str());
        if(!doc)
            throw std::runtime_error("Syntax error in "+fname);

        if(!doc->children)
            throw std::runtime_error("Semantic error in "+fname);

        ctx = xmlXPathNewContext(doc);
        if(!ctx)
            throw std::runtime_error("Unable to create XPath context");

        params.fname0 = get_data(ctx, "//project/images/@image1",params.fname0);
        params.fname1 = get_data(ctx, "//project/images/@image2",params.fname1);

        // circumvent MdiEditor's notion that relative files must begin with '/'
        if(!params.fname0.empty() && (params.fname0[0] == '/' || params.fname0[0] == '\\'))
            params.fname0 = params.fname0.substr(1);
        if(!params.fname1.empty() && (params.fname1[0] == '/' || params.fname1[0]=='\\'))
            params.fname1 = params.fname1.substr(1);

	int slash = fname.find_last_of("/\\");
	std::string root_path;

	if(slash != fname.npos)
	{
	    root_path = fname.substr(0,slash+1); // root_path includes final slash
	    params.fname0 = root_path + params.fname0;
	    params.fname1 = root_path + params.fname1;
	}

        std::string base = "/project/layers";

        params.w_tps
            = get_data(ctx, base+"/l0/parameters/weight/@tps", params.w_tps);

        params.w_ssim
            = get_data(ctx, base+"/l0/parameters/weight/@ssim", params.w_ssim);

        params.w_ui
            = get_data(ctx, base+"/l0/parameters/weight/@ui", params.w_ui);

        params.ssim_clamp
            = 1-get_data(ctx, base+"/l0/parameters/weight/@ssimclamp", 1-params.ssim_clamp);

        int bound;
        switch(params.bcond)
        {
        case BCOND_NONE:
            bound = 0;
            break;
        case BCOND_CORNER:
            bound = 1;
            break;
        case BCOND_BORDER:
            bound = 2;
            break;
        }

        bound = get_data(ctx, base+"/l0/parameters/boundary/@lock", bound);

        switch(bound)
        {
        case 0:
            params.bcond = BCOND_NONE;
            break;
        case 1:
            params.bcond = BCOND_CORNER;
            break;
        case 2:
            params.bcond = BCOND_BORDER;
            break;
        default:
            throw std::runtime_error("Bad boundary value");
        }

        params.eps
            = get_data(ctx, base+"/l0/parameters/debug/@eps", params.eps);
        params.start_res
            = get_data(ctx, base+"/l0/parameters/debug/@startres", params.start_res);
        params.max_iter
            = get_data(ctx, base+"/l0/parameters/debug/@iternum", params.max_iter);
        params.max_iter_drop_factor
            = get_data(ctx, base+"/l0/parameters/debug/@dropfactor", params.max_iter_drop_factor);

        std::string pts0
            = get_data(ctx, base+"/l0/parameters/points/@image1", "");

        std::string pts1
            = get_data(ctx, base+"/l0/parameters/points/@image2", "");

        if(!pts0.empty())
        {
            params.ui_points.clear();

            std::istringstream ss0(pts0), ss1(pts1);

            while(ss0 && ss1)
            {
                ConstraintPoint cpt;

                float2 pt;
                ss0 >> pt.x >> pt.y;
                cpt.lp = make_double2(pt.x, pt.y);

                ss1 >> pt.x >> pt.y;
                cpt.rp = make_double2(pt.x, pt.y);

                if(ss0 && ss1)
                    params.ui_points.push_back(cpt);
            }

            if(ss0.eof() && !ss1.eof() || !ss0.eof() && ss1.eof())
                throw std::runtime_error("Control point parsing error");
        }

        xmlXPathFreeContext(ctx);
        xmlFreeDoc(doc);
    }
Пример #4
0
 __inline__ __device__ static
 type texture2type(const texture_type& value)
 {
   return make_double2(__hiloint2double(value.y, value.x),
                       __hiloint2double(value.w, value.z));
 }
Пример #5
0
//multiplication
inline __host__ __device__ double2 operator*(double2 in1, double2 in2)
{
  return make_double2(in1.x * in2.x - in1.y * in2.y, in1.x * in2.y + in1.y * in2.x);
}
Пример #6
0
inline __host__ __device__ double2 operator/(double2 a,SCALARTYPE b)
{
  return make_double2(a.x/b, a.y/b);
}
Пример #7
0
// subtraction
inline __host__ __device__ double2 operator-(double2 a, double2 b)
{
  return make_double2(a.x - b.x, a.y - b.y);
}