static uint16 getNumFromCharArr(char * str, uint8 len) { uint16 cData = 0; for (uint8 i = 1; i < len; i++) { cData += (str[i] - 48) * e10((len - i) - 1); } }
// Test face for (almost) convexeness. Allows certain convexity before // complaining. // For every two consecutive edges calculate the normal. If it differs too // much from the average face normal complain. bool Foam::combineFaces::convexFace ( const scalar minConcaveCos, const pointField& points, const face& f ) { // Get outwards pointing normal of f. vector n = f.normal(points); n /= mag(n); // Get edge from f[0] to f[size-1]; vector ePrev(points[f.first()] - points[f.last()]); scalar magEPrev = mag(ePrev); ePrev /= magEPrev + VSMALL; forAll(f, fp0) { // Get vertex after fp label fp1 = f.fcIndex(fp0); // Normalized vector between two consecutive points vector e10(points[f[fp1]] - points[f[fp0]]); scalar magE10 = mag(e10); e10 /= magE10 + VSMALL; if (magEPrev > SMALL && magE10 > SMALL) { vector edgeNormal = ePrev ^ e10; if ((edgeNormal & n) < 0) { // Concave. Check angle. if ((ePrev & e10) < minConcaveCos) { return false; } } } ePrev = e10; magEPrev = magE10; }
int ie10(int a) { int i; for (i=0;e10(i)<=a;i++) ; return i-1; }
main() { Citygraph g; Cityvertex v[6]; Map<String,Vptr> m; m["SFRAN"] = &v[0]; v[0].id = "SFRAN"; m["LA"] = &v[1]; v[1].id = "LA"; m["CHGO"] = &v[2]; v[2].id = "CHGO"; m["DLLS"] = &v[3]; v[3].id = "DLLS"; m["BOST"] = &v[4]; v[4].id = "BOST"; m["NYC"] = &v[5]; v[5].id = "NYC"; Cityedge e0(m["CHGO"], m["BOST"], 0); Cityedge e1(m["SFRAN"], m["LA"], 1); Cityedge e2(m["LA"], m["SFRAN"], 2); Cityedge e3(m["SFRAN"], m["CHGO"], 3); Cityedge e4(m["SFRAN"], m["NYC"], 4); Cityedge e5(m["SFRAN"], m["BOST"], 5); Cityedge e6(m["BOST"], m["SFRAN"], 6); Cityedge e7(m["NYC"], m["SFRAN"], 7); Cityedge e8(m["CHGO"], m["SFRAN"], 8); Cityedge e9(m["DLLS"], m["NYC"], 9); Cityedge e10(m["BOST"], m["LA"], 10); g.insert(&e0); g.insert(&e1); g.insert(&e2); g.insert(&e3); g.insert(&e4); g.insert(&e5); g.insert(&e6); g.insert(&e7); g.insert(&e8); g.insert(&e9); g.insert(&e10); Set_of_p<Cityvertex> vpset; vpset.insert(m["SFRAN"]); vpset.insert(m["LA"]); vpset.insert(m["BOST"]); vpset.insert(m["NYC"]); vpset.insert(m["CHGO"]); Citygraph g2 = g.induced_graph(vpset); //create the subgraph of these Vertices and appropriate Edges // Now let's verify what is in the newly-created subgraph for (int i = 0; i < 6; i++) cout << v[i].id << ": " << g2.contains(&v[i]) << "\n" << flush; cout << "e with id " << e0.id << ": "<< g2.contains(&e0)<< "\n"<< flush; cout << "e with id " << e1.id << ": "<< g2.contains(&e1)<< "\n"<< flush; cout << "e with id " << e2.id << ": "<< g2.contains(&e2)<< "\n"<< flush; cout << "e with id " << e3.id << ": "<< g2.contains(&e3)<< "\n"<< flush; cout << "e with id " << e4.id << ": "<< g2.contains(&e4)<< "\n"<< flush; cout << "e with id " << e5.id << ": "<< g2.contains(&e5)<< "\n"<< flush; cout << "e with id " << e6.id << ": "<< g2.contains(&e6)<< "\n"<< flush; cout << "e with id " << e7.id << ": "<< g2.contains(&e7)<< "\n"<< flush; cout << "e with id " << e8.id << ": "<< g2.contains(&e8)<< "\n"<< flush; cout << "e with id " << e9.id << ": "<< g2.contains(&e9)<< "\n"<< flush; cout << "e with id " << e10.id << ": "<< g2.contains(&e10)<< "\n"<< flush; return(0); }
int phypp_main(int argc, char* argv[]) { if (argc < 2) { print("usage: make_shifts <ob_filter> [options]"); return 0; } vec1u exclude; std::string helper; read_args(argc-1, argv+1, arg_list(exclude, helper)); if (helper.empty()) { error("please provide the name of (one of) the helper target you used to " "calibrate the shifts (helper=...)"); return 1; } helper = tolower(helper); std::string scis = argv[1]; vec1d cent_ra, cent_dec; file::read_table("centroid_helper.txt", 0, cent_ra, cent_dec); vec1u ids = uindgen(cent_ra.size())+1; vec1u idex = where(is_any_of(ids, exclude)); inplace_remove(ids, idex); inplace_remove(cent_ra, idex); inplace_remove(cent_dec, idex); std::ofstream cmb("combine.sof"); vec1d shx, shy; vec1d x0, y0; bool first_line = true; uint_t nexp = 0; for (uint_t i : range(ids)) { std::string dir = scis+align_right(strn(ids[i]), 2, '0')+"/"; print(dir); vec1s files = dir+file::list_files(dir+"sci_reconstructed*-sci.fits"); inplace_sort(files); // Find out which exposures contain the helper target from which the // shifts were calibrated vec1u ignore; for (uint_t k : range(files)) { std::string f = files[k]; fits::generic_file fcubes(f); vec1s arms(24); bool badfile = false; for (uint_t u : range(24)) { if (!fcubes.read_keyword("ESO OCS ARM"+strn(u+1)+" NAME", arms[u])) { note("ignoring invalid file '", f, "'"); note("missing keyword 'ESO OCS ARM"+strn(u+1)+" NAME'"); ignore.push_back(k); badfile = true; break; } } if (badfile) continue; arms = tolower(trim(arms)); vec1u ida = where(arms == helper); if (ida.empty()) { ignore.push_back(k); } else if (x0.empty()) { // Get astrometry of IFUs from first exposure // NB: assumes the rotation is the same for all exposures, // which is anyway what kmos_combine does later on. fcubes.reach_hdu(ida[0]+1); fits::wcs astro(fcubes.read_header()); fits::ad2xy(astro, cent_ra, cent_dec, x0, y0); } } inplace_remove(files, ignore); if (files.empty()) { warning("folder ", dir, " does not contain any usable file"); continue; } for (std::string f : files) { cmb << f << " COMMAND_LINE\n"; ++nexp; } double dox = x0[0] - x0[i], doy = y0[0] - y0[i]; if (first_line) { // Ommit first line which has, by definition, no offset first_line = false; } else { shx.push_back(dox); shy.push_back(doy); } if (file::exists(dir+"helpers/shifts.txt")) { vec1d tx, ty; file::read_table(dir+"helpers/shifts.txt", 0, tx, ty); for (uint_t j : range(tx)) { shx.push_back(dox+tx[j]); shy.push_back(doy+ty[j]); } } } note("found ", nexp, " exposures"); cmb.close(); auto truncate_decimals = vectorize_lambda([](double v, uint_t nd) { return long(v*e10(nd))/e10(nd); }); shx = truncate_decimals(shx, 2); shy = truncate_decimals(shy, 2); file::write_table("shifts.txt", 10, shx, shy); return 0; }
DemoMesh::DemoMesh(const char* const name, dFloat* const elevation, int size, dFloat cellSize, dFloat texelsDensity, int tileSize) :DemoMeshInterface() ,dList<DemoSubMesh>() ,m_uv(NULL) ,m_vertex(NULL) ,m_normal(NULL) ,m_optimizedOpaqueDiplayList(0) ,m_optimizedTransparentDiplayList(0) { dFloat* elevationMap[4096]; dVector* normalMap[4096]; dFloat* const normalsPtr = new dFloat [size * size * 4]; // dVector* const normals = new dVector [size * size]; dVector* const normals = (dVector*)normalsPtr; for (int i = 0; i < size; i ++) { elevationMap[i] = &elevation[i * size]; normalMap[i] = &normals[i * size]; } memset (normals, 0, (size * size) * sizeof (dVector)); for (int z = 0; z < size - 1; z ++) { for (int x = 0; x < size - 1; x ++) { dVector p0 ((x + 0) * cellSize, elevationMap[z + 0][x + 0], (z + 0) * cellSize); dVector p1 ((x + 1) * cellSize, elevationMap[z + 0][x + 1], (z + 0) * cellSize); dVector p2 ((x + 1) * cellSize, elevationMap[z + 1][x + 1], (z + 1) * cellSize); dVector p3 ((x + 0) * cellSize, elevationMap[z + 1][x + 0], (z + 1) * cellSize); dVector e10 (p1 - p0); dVector e20 (p2 - p0); dVector n0 (e20.CrossProduct(e10)); n0 = n0.Scale ( 1.0f / dSqrt (n0.DotProduct3(n0))); normalMap [z + 0][x + 0] += n0; normalMap [z + 0][x + 1] += n0; normalMap [z + 1][x + 1] += n0; dVector e30 (p3 - p0); dVector n1 (e30.CrossProduct(e20)); n1 = n1.Scale ( 1.0f / dSqrt (n1.DotProduct3(n1))); normalMap [z + 0][x + 0] += n1; normalMap [z + 1][x + 0] += n1; normalMap [z + 1][x + 1] += n1; } } for (int i = 0; i < size * size; i ++) { normals[i] = normals[i].Scale (1.0f / dSqrt (normals[i].DotProduct3(normals[i]))); } AllocVertexData (size * size); dFloat* const vertex = m_vertex; dFloat* const normal = m_normal; dFloat* const uv = m_uv; int index0 = 0; for (int z = 0; z < size; z ++) { for (int x = 0; x < size; x ++) { vertex[index0 * 3 + 0] = x * cellSize; vertex[index0 * 3 + 1] = elevationMap[z][x]; vertex[index0 * 3 + 2] = z * cellSize; normal[index0 * 3 + 0] = normalMap[z][x].m_x; normal[index0 * 3 + 1] = normalMap[z][x].m_y; normal[index0 * 3 + 2] = normalMap[z][x].m_z; uv[index0 * 2 + 0] = x * texelsDensity; uv[index0 * 2 + 1] = z * texelsDensity; index0 ++; } } int segmentsCount = (size - 1) / tileSize; for (int z0 = 0; z0 < segmentsCount; z0 ++) { int z = z0 * tileSize; for (int x0 = 0; x0 < segmentsCount; x0 ++ ) { int x = x0 * tileSize; DemoSubMesh* const tile = AddSubMesh(); tile->AllocIndexData (tileSize * tileSize * 6); unsigned* const indexes = tile->m_indexes; //strcpy (tile->m_textureName, "grassanddirt.tga"); tile->m_textureName = "grassanddirt.tga"; tile->m_textureHandle = LoadTexture(tile->m_textureName.GetStr()); int index1 = 0; int x1 = x + tileSize; int z1 = z + tileSize; for (int z2 = z; z2 < z1; z2 ++) { for (int x2 = x; x2 < x1; x2 ++) { int i0 = x2 + 0 + (z2 + 0) * size; int i1 = x2 + 1 + (z2 + 0) * size; int i2 = x2 + 1 + (z2 + 1) * size; int i3 = x2 + 0 + (z2 + 1) * size; indexes[index1 + 0] = i0; indexes[index1 + 1] = i2; indexes[index1 + 2] = i1; indexes[index1 + 3] = i0; indexes[index1 + 4] = i3; indexes[index1 + 5] = i2; index1 += 6; } } } } delete[] normalsPtr; OptimizeForRender(); }
int json_cpp_tests() { json::Value e1(json::load_file("test.json")); json::Value e2(e1); json::Value e3; json::Value e4(json::load_string("{\"foo\": true, \"bar\": \"test\"}")); ASSERT_TRUE(e1.is_object(), "e1 is not an object"); ASSERT_TRUE(e2.is_object(), "e2 is not an object"); ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value"); ASSERT_TRUE(e4.is_object(), "e4 is not an object"); ASSERT_EQ(e1.size(), 1, "e1 has too many properties"); ASSERT_EQ(e2.size(), 1, "e2 has too many properties"); ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements"); ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object"); ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value"); // verify iterator results (note that they can be returned in any order) json::Iterator i(e1.get("web-app")); std::set<std::string> iteratorResults; for ( int ii = 0; ii < 3; ++ii ) { ASSERT_FALSE(i.key().empty(), "iterator returned a null value"); iteratorResults.insert(i.key()); i.next(); } ASSERT_FALSE(i.valid(), "iterator has more values than expected"); ASSERT_EQ(iteratorResults.size(), 3, "iterator did not return enough values"); json::Value e5(json::Value(12.34)); ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment"); ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment"); json::Value e6(json::Value(true)); ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment"); ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment"); json::Value e7(json::Value("foobar")); ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment"); ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment"); json::Value e8(json::object()); ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment"); json::Value e9(json::null()); ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment"); json::Value e10(json::array()); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); e10.set_at(0, json::Value("foobar")); ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment"); e10.set_at(1, json::Value("foobar")); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment"); e10.set_at(0, json::Value("barfoo")); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment"); e10.set_at(100, json::null()); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); e10.insert_at(1, json::Value("new")); ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert"); ASSERT_EQ(e10[1].as_string(), "new", "e10[1] has incorrect value after insert"); ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert"); e10.del_at(0); ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete"); ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete"); e10.clear(); ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear"); json::Value e11(json::object()); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); e11.set_key("foo", json::Value("test")); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment"); e11.set_key("foo", json::Value("again")); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment"); e11.set_key("bar", json::Value("test")); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment"); e11.clear(); ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear"); json::Value e12(json::object()); e12.set_key("foo", json::Value("test")); e12.set_key("bar", json::Value(3)); char* out_cstr = e12.save_string(JSON_COMPACT); std::string out(out_cstr); free(out_cstr); ASSERT_EQ(out, "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); std::istringstream instr(out); instr >> e12; ASSERT_TRUE(e12.is_object(), "e12 is not an object after stream read"); ASSERT_EQ(e12.size(), 2, "e12 has wrong size after stream read"); ASSERT_EQ(e12.get("bar").as_integer(), 3, "e12.bar has incorrect value after stream read"); ASSERT_EQ(e12.get("foo").as_string(), "test", "ee12.test has incorrect value after stream read"); std::ostringstream outstr; outstr << e12; ASSERT_EQ(instr.str(), "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); const json::Value e13(e12); ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy"); json::Value e14(json::object()); ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction"); e14.set_key("foo", json::object()); ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment"); e14["foo"]["bar"] = json::Value(42); ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment"); json::Value e15(json::array()); ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction"); e15.set_at(0, json::Value(42)); ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment"); e15[0] = json::Value("foo"); ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment"); return 0; }