bool ossimPlanetViewer::pickAtWindowCoordinate(PickList& result, double wx, double wy, osg::Node::NodeMask traversalMask) { result.clear(); osgUtil::LineSegmentIntersector::Intersections intersections; if (computeIntersections(wx, wy, intersections, traversalMask)) { osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); while(hitr != intersections.end()) { osg::Vec3d wpt = hitr->getWorldIntersectPoint(); osg::Vec3d llh; if(model()) { model()->inverse(wpt, llh); } result.push_back(new PickObject(hitr->nodePath, hitr->getLocalIntersectPoint(), hitr->getWorldIntersectPoint(), llh)); ++hitr; } } return !result.empty(); }
void SimpleSweepLineIntersector::computeIntersections(vector<Edge*> *edges0,vector<Edge*> *edges1,SegmentIntersector *si) { add(edges0,edges0); add(edges1,edges1); computeIntersections(si); }
void SimpleSweepLineIntersector::computeIntersections(vector<Edge*> *edges, SegmentIntersector *si, bool testAllSegments) { if (testAllSegments) add(edges,nullptr); else add(edges); computeIntersections(si); }
std::vector<std::pair<std::pair<DocId, DocId>, double>> computeSimilarities(std::unordered_map<DocId, Document> docs) { DocSimilarities result = computeIntersections(docs); result.forEach([&docs, &result] (DocId id1, DocId id2, double similarity) mutable { double docsUnion = docs.at(id1).data.size() + docs.at(id2).data.size() - similarity; result.setSimilarity(id1, id2, similarity / docsUnion); }); return result.pairs(); }
int ProjectShell::project() { double dist1= dist(m_direction); if (dist1==0) { std::cerr << " null direction \n"; return 1; } // normalize direction for (int k=0; k<3; k++) { m_direction[k]/=dist1; } int ret = getMeshData(); if (ret) { std::cout<< " bad mesh\n" ; return 1; } // we have now the 3D mesh // verify orientation of the triangles; is it manifold? ret = checkMeshValidity(); if (ret) { std::cout<< " bad orientation\n" ; return 1; } ret = projectIn2D(); if (ret) { std::cout<< " cannot project in 2d\n" ; return 1; } ret = computeIntersections(); if (ret) { std::cout<< " error in computing intersections\n" ; return 1; } return 0; }
bool ossimPlanetViewer::getLatLonHeightAtWindowCoordinate(osg::Vec3d& llh, double wx, double wy, osg::Node::NodeMask traversalMask) { bool resultFlag = false; osgUtil::LineSegmentIntersector::Intersections intersections; if (computeIntersections(wx, wy, intersections, traversalMask)) { osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); if(hitr != intersections.end()) { osg::Vec3d wpt = hitr->getWorldIntersectPoint(); if(model()) { model()->inverse(wpt, llh); resultFlag = true; } } } return resultFlag; }
/// This is our compute-routine int coReadSTP3::compute(const char *) { char buf[1024]; const char *path = pbrVolumeFile->getValue(); FILE *fp = fopen(path, "rb"); if (!fp) { sprintf(buf, "Failed to open file %s", path); sendInfo(buf); return STOP_PIPELINE; } float ignore_value = pfsIgnoreValue->getValue(); int32_t image_type; if (fread(&image_type, sizeof(image_type), 1, fp) != sizeof(image_type)) { fprintf(stderr, "fread_1 failed in ReadSTP3.cpp"); } byteSwap(image_type); int32_t series_header; if (fread(&series_header, sizeof(series_header), 1, fp) != sizeof(series_header)) { fprintf(stderr, "fread_2 failed in ReadSTP3.cpp"); } byteSwap(series_header); int32_t image_header; if (fread(&image_header, sizeof(image_header), 1, fp) != sizeof(image_header)) { fprintf(stderr, "fread_3 failed in ReadSTP3.cpp"); } byteSwap(image_header); int32_t image_length; if (fread(&image_length, sizeof(image_length), 1, fp) != sizeof(image_length)) { fprintf(stderr, "fread_4 failed in ReadSTP3.cpp"); } byteSwap(image_length); char patient_name[81]; if (fread(patient_name, 1, 80, fp) != 1) { fprintf(stderr, "fread_5 failed in ReadSTP3.cpp"); } patient_name[80] = '\0'; char comment[81]; if (fread(comment, 1, 80, fp) != 1) { fprintf(stderr, "fread_6 failed in ReadSTP3.cpp"); } comment[80] = '\0'; if (fread(&resolution, sizeof(resolution), 1, fp) != sizeof(resolution)) { fprintf(stderr, "fread_7 failed in ReadSTP3.cpp"); } byteSwap(resolution); int32_t byte_per_voxel; if (fread(&byte_per_voxel, sizeof(byte_per_voxel), 1, fp) != sizeof(byte_per_voxel)) { fprintf(stderr, "fread_8 failed in ReadSTP3.cpp"); } byteSwap(byte_per_voxel); if (fread(&num_slices, sizeof(num_slices), 1, fp) != sizeof(num_slices)) { fprintf(stderr, "fread_9 failed in ReadSTP3.cpp"); } byteSwap(num_slices); double psiz; if (fread(&psiz, sizeof(psiz), 1, fp) != sizeof(psiz)) { fprintf(stderr, "fread_10 failed in ReadSTP3.cpp"); } byteSwap(psiz); pixel_size = (float)psiz; char date[81]; if (fread(date, 1, 80, fp) != 1) { fprintf(stderr, "fread_11 failed in ReadSTP3.cpp"); } date[80] = '\0'; char *image_type_desc = "(unknown)"; switch (image_type) { case 1: case 100: image_type = 1; image_type_desc = "CT"; break; case 2: case 200: image_type = 2; image_type_desc = "MR"; break; case 3: case 410: image_type = 3; image_type_desc = "PET"; break; } sprintf(buf, "Reading %s: %s image, %dx%d pixels, %d slices, %d byte/voxel", path, image_type_desc, (int)resolution, (int)resolution, (int)num_slices, (int)byte_per_voxel); sendInfo(buf); sprintf(buf, "Patient: %s", patient_name); sendInfo(buf); sprintf(buf, "Comment: %s", comment); sendInfo(buf); //sprintf(buf, "Date: %s", date); //sendInfo(buf); slice_z = new float[num_slices]; FILE *vfp = NULL; int32_t voi_header; bool read_voi = pboUseVoi->getValue(); voi_total_no = 0; if (read_voi) { char *voiPath = new char[strlen(path) + 5]; strcpy(voiPath, path); char *ext = strrchr(voiPath, '.'); if (ext) { strcpy(ext, ".vois"); } else { strcat(voiPath, ".vois"); } vfp = fopen(voiPath, "rb"); if (!vfp) { sprintf(buf, "Failed to open voi file %s", voiPath); sendInfo(buf); read_voi = false; } else { int32_t voi_version_number; if (fread(&voi_version_number, sizeof(voi_version_number), 1, vfp) != sizeof(voi_version_number)) { fprintf(stderr, "fread_12 failed in ReadSTP3.cpp"); } // 340 byteSwap(voi_version_number); if (fread(&voi_header, sizeof(voi_header), 1, vfp) != sizeof(voi_header)) { fprintf(stderr, "fread_13 failed in ReadSTP3.cpp"); } // 2048 byteSwap(voi_header); //fprintf(stderr, "voi_header=%d\n", voi_header); char voi_patient_name[81]; if (fread(voi_patient_name, 1, 80, vfp) != 1) { fprintf(stderr, "fread_14 failed in ReadSTP3.cpp"); } voi_patient_name[80] = '\0'; char dummy_name[81]; if (fread(dummy_name, 1, 80, vfp) != 1) { fprintf(stderr, "fread_15 failed in ReadSTP3.cpp"); } // empty if (fread(&voi_total_no, sizeof(voi_total_no), 1, vfp) != sizeof(voi_total_no)) { fprintf(stderr, "fread_16 failed in ReadSTP3.cpp"); } // 20 byteSwap(voi_total_no); int32_t voi_slices; if (fread(&voi_slices, sizeof(voi_slices), 1, vfp) != sizeof(voi_slices)) { fprintf(stderr, "fread_17 failed in ReadSTP3.cpp"); } byteSwap(voi_slices); sprintf(buf, "VOI Patient: %s", voi_patient_name); sendInfo(buf); sprintf(buf, "No. of vois: %d, no. of slices for vois: %d", voi_total_no, voi_slices); sendInfo(buf); } } int voi_num = pisVoiNo->getValue(); int32_t voi_property, voi_first_slice, voi_last_slice, voi_color; char voi_name[41]; if (read_voi && voi_num < MAX_VOIS && voi_num < voi_total_no) { fseek(vfp, voi_header + VOI_DESC_SIZE * voi_num, SEEK_SET); if (fread(&voi_property, sizeof(voi_property), 1, vfp) != sizeof(voi_property)) { fprintf(stderr, "fread_18 failed in ReadSTP3.cpp"); } byteSwap(voi_property); if (fread(voi_name, 40, 1, vfp) != 40) { fprintf(stderr, "fread_19 failed in ReadSTP3.cpp"); } voi_name[40] = '\0'; if (fread(&voi_first_slice, sizeof(voi_first_slice), 1, vfp) != sizeof(voi_first_slice)) { fprintf(stderr, "fread_20 failed in ReadSTP3.cpp"); } byteSwap(voi_first_slice); if (fread(&voi_last_slice, sizeof(voi_last_slice), 1, vfp) != sizeof(voi_last_slice)) { fprintf(stderr, "fread_21 failed in ReadSTP3.cpp"); } byteSwap(voi_last_slice); if (fread(&voi_color, sizeof(voi_color), 1, vfp) != sizeof(voi_color)) { fprintf(stderr, "fread_22 failed in ReadSTP3.cpp"); } byteSwap(voi_color); fprintf(stderr, "voi name: %s, first=%d, last=%d\n", voi_name, voi_first_slice, voi_last_slice); } else { read_voi = false; } if (read_voi) { voi_first_slice--; voi_last_slice--; } else { voi_first_slice = 0; voi_last_slice = num_slices - 1; } coDoFloat *dataOut = new coDoFloat(poVolume->getObjName(), resolution * resolution * num_slices); float *data = NULL; dataOut->getAddress(&data); size_t slice_size = resolution * resolution * byte_per_voxel; unsigned char *slice = new unsigned char[slice_size]; float minZ = -1.0, maxZ = 1.0; for (int i = 0; i < num_slices; i++) { long fpos = series_header + i * image_header + i * image_length; fseek(fp, fpos, SEEK_SET); int32_t image_type; if (fread(&image_type, sizeof(image_type), 1, fp) != sizeof(image_type)) { fprintf(stderr, "fread_23 failed in ReadSTP3.cpp"); } byteSwap(image_type); double z_pos; if (fread(&z_pos, sizeof(double), 1, fp) != sizeof(double)) { fprintf(stderr, "fread_24 failed in ReadSTP3.cpp"); } byteSwap(z_pos); slice_z[i] = z_pos; float z_position = (float)z_pos; double gantry; if (fread(&gantry, sizeof(double), 1, fp) != sizeof(double)) { fprintf(stderr, "fread_25 failed in ReadSTP3.cpp"); } byteSwap(gantry); //float gantry_tilt = (float)gantry; if (i == 0) { minZ = z_position; } else if (i == num_slices - 1) { maxZ = z_position; } } if (read_voi) { fseek(vfp, voi_header + VOI_DESC_SIZE * MAX_VOIS, SEEK_SET); //fprintf(stderr, "pos=%d\n", voi_header+VOI_DESC_SIZE*MAX_VOIS); } for (int i = 0; i < num_slices; i++) { vector<float> p_x, p_y; readVoiSlice(vfp, voi_num, &p_x, &p_y); long fpos = series_header + (i + 1) * image_header + i * image_length; #if 0 if(minZ > maxZ) { fpos = series_header + (num_slices-1-i-1)*image_header + (num_slices-1-i)*image_length; } #endif fseek(fp, fpos, SEEK_SET); if (fread(slice, slice_size, 1, fp) != slice_size) { fprintf(stderr, "fread_26 failed in ReadSTP3.cpp"); } for (int y = 0; y < resolution; y++) { vector<float> i_x; computeIntersections(y, p_x, p_y, &i_x); int no_isect = 0; for (int x = 0; x < resolution; x++) { while (no_isect < i_x.size() && i_x[no_isect] < x) no_isect++; int j = x * resolution + y; int k = ((y * resolution) + x) * num_slices + i; if (byte_per_voxel == 1) { data[k] = slice[j] / 255.f; } else if (byte_per_voxel == 2) { data[k] = (256.f * slice[j * 2] + slice[j * 2 + 1]) / 65535.f; } else { data[k] = 0.f; } if (read_voi && no_isect % 2 == 0) { data[k] = ignore_value; } } } p_x.clear(); p_y.clear(); } if (fp) fclose(fp); coDoFloat *voiOut[NO_VOIS]; for (int v = 0; v < NO_VOIS; v++) { if (!poVoi[v]->isConnected()) { voiOut[v] = NULL; continue; } voiOut[v] = new coDoFloat(poVoi[v]->getObjName(), resolution * resolution * num_slices); float *data = NULL; voiOut[v]->getAddress(&data); voi_num = pisVolumeFromVoi[v]->getValue(); if (read_voi && voi_num < MAX_VOIS && voi_num < voi_total_no) { fseek(vfp, voi_header + VOI_DESC_SIZE * voi_num, SEEK_SET); if (fread(&voi_property, sizeof(voi_property), 1, vfp) != sizeof(voi_property)) { fprintf(stderr, "fread_27 failed in ReadSTP3.cpp"); } byteSwap(voi_property); if (fread(voi_name, 40, 1, vfp) != 40) { fprintf(stderr, "fread_28 failed in ReadSTP3.cpp"); } voi_name[40] = '\0'; if (fread(&voi_first_slice, sizeof(voi_first_slice), 1, vfp) != sizeof(voi_first_slice)) { fprintf(stderr, "fread_29 failed in ReadSTP3.cpp"); } byteSwap(voi_first_slice); if (fread(&voi_last_slice, sizeof(voi_last_slice), 1, vfp) != sizeof(voi_last_slice)) { fprintf(stderr, "fread_30 failed in ReadSTP3.cpp"); } byteSwap(voi_last_slice); if (fread(&voi_color, sizeof(voi_color), 1, vfp) != sizeof(voi_color)) { fprintf(stderr, "fread_31 failed in ReadSTP3.cpp"); } byteSwap(voi_color); fprintf(stderr, "voi name: %s, first=%d, last=%d\n", voi_name, voi_first_slice, voi_last_slice); fseek(vfp, voi_header + VOI_DESC_SIZE * MAX_VOIS, SEEK_SET); for (int i = 0; i < num_slices; i++) { vector<float> p_x, p_y; readVoiSlice(vfp, voi_num, &p_x, &p_y); for (int y = 0; y < resolution; y++) { vector<float> i_x; computeIntersections(y, p_x, p_y, &i_x); int no_isect = 0; for (int x = 0; x < resolution; x++) { while (no_isect < i_x.size() && i_x[no_isect] < x) no_isect++; int k = ((y * resolution) + x) * num_slices + i; if (no_isect % 2 == 0) { data[k] = ignore_value; } else { data[k] = 1.f; } } } } } } if (vfp) fclose(vfp); delete[] slice; float maxX = pixel_size * resolution / 2.f; float minX = -maxX; float maxY = maxX; float minY = -maxY; #if 0 if(minZ > maxZ) { float temp = minZ; minZ = maxZ; maxZ = temp; } #endif coDoUniformGrid *gridOut = new coDoUniformGrid(poGrid->getObjName(), resolution, resolution, num_slices, //voi_last_slice-voi_first_slice+1, minX, maxX, minY, maxY, //minZ + ((maxZ-minZ)*voi_first_slice)/num_slices, minZ + ((maxZ-minZ)*voi_last_slice)/num_slices); minZ, maxZ); char *matPath = new char[strlen(path) + 5]; strcpy(matPath, path); char *ext = strrchr(matPath, '.'); if (ext) { strcpy(ext, ".tra"); } else { strcat(matPath, ".tra"); } double mat[4][4], inv[4][4]; if (getTransformation(matPath, &mat[0][0], &inv[0][0]) >= 0) { char transMat[64 * 16]; char *p = transMat; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { int sz = sprintf(p, "%f ", mat[j][i]); p += sz; } } gridOut->addAttribute("Transformation", transMat); fprintf(stderr, "attached Transformation: %s\n", transMat); } else { char buf[1024]; sprintf(buf, "failed to read transformation data from %s", matPath); sendInfo(matPath); } poGrid->setCurrentObject(gridOut); poVolume->setCurrentObject(dataOut); sprintf(buf, "Volume data loaded: (%f, %f, %f) - (%f, %f %f)", minX, minY, minZ, maxX, maxY, maxZ); sendInfo(buf); return CONTINUE_PIPELINE; }