I want to simplify or edge collapse a mesh read from .off file as a combinatorial map using CGAL
std::ifstream ifile(fileName.toStdString().c_str());
if (ifile)
{
CGAL::load_off(lcc, ifile);
lcc.display_characteristics(std::cout)<<", is_valid="<<CGAL::is_valid(lcc)<<std::endl;
}
namespace SMS = CGAL::Surface_mesh_simplification ;
SMS::Count_stop_predicate<LCC> stop(lcc.number_of_halfedges()/2 - 1);
int r = SMS::edge_collapse
(lcc
,stop
,CGAL::parameters::halfedge_index_map(get(CGAL::halfedge_index, lcc))
.vertex_index_map(get(boost::vertex_index, lcc))
.get_cost(SMS::Edge_length_cost<LCC>())
.get_placement(SMS::Midpoint_placement<LCC>())
);
std::cout << "\nFinished...\n" << r << " edges removed.\n"
<< (lcc.number_of_darts()/2) << " final edges.\n" ;
lcc.display_characteristics(std::cout)<<", is_valid="<<CGAL::is_valid(lcc)<<std::endl;
the output :
#Darts=16674, #0-cells=2775, #1-cells=8337, #2-cells=5558, #ccs=1, is_valid=1
Finished...
0 edges removed.
8337 final edges.
#Darts=16674, #0-cells=2775, #1-cells=8337, #2-cells=5558, #ccs=1, is_valid=1
the method do nothing , I tried more than .off file and it's preview it properly but it cannot simplify it
I appreciate any help .
See the example given here, it works perfectly.
Related
I am trying to learn ROOT and I have a few codes that I can work with. Sometimes codes work but sometimes they don't.
{
c1 = new TCanvas("c1", "My Root Plots",600, 400);
c1->Divide(2,2);
c1->cd(1);
f=new TF1("f","[0]*exp(-0.5*((x-[1])/[2])**2)/(sqrt(2.0*TMath::Pi())*[2])",-100,100); f->SetTitle("Gaus;X axis ;Y axis");
f->SetParameter(0,0.5*sqrt(2*TMath::Pi()));
f->SetParameter(1,8);
f->SetParameter(2,5);
f->SetLineColor(3);
f->SetMarkerColor(1);
f->SetMarkerStyle(kOpenStar);
f->SetMarkerSize(5);
f->Draw();
c1->cd(2);
f1 = new TF1("f1", "[0]*x+[1]", 0,50);
f1->SetParameters(10,4);
f1->SetLineColor(5);
f1->SetTitle("ax+b;x;y");
f1->Draw();
}
This is the code I am trying to do. Code is kinda working , ''what do you mean kinda working''. I mean it's giving me a graph but as you can see in the code I wrote ( f->SetMarkerColor(1);
f->SetMarkerStyle(kOpenStar);) But markers didn't appear on the graph. Terminal doesn't giving me any errors. Is it my ROOT library missing ? I cannot upload images because I am new here.
I have a another problem. I want to share it maybe it will help solving the problem that I have.
void testRandom(Int_t nrEvents=500000000)
{
TRandom *r1=new TRandom();
TRandom2 *r2=new TRandom2();
TRandom3 *r3=new TRandom3();
TCanvas* c1=new TCanvas("c1","TRandom Number Generators", 800,600); c1->Divide(3,1);
TH1D *h1=new TH1D("h1","TRandom",500,0,1); TH1D *h2=new TH1D("h2","TRandom2",500,0,1); TH1D *h3=new TH1D("h3","TRandom3",500,0,1); TStopwatch *st=new TStopwatch();
st->Start();
for (Int_t i=0; i<nrEvents; i++) { h1->Fill(r1->Uniform(0,1)); } st->Stop(); cout << "Random: " << st->CpuTime() << endl; st->Start();
c1->cd(1); h1->SetFillColor(kRed+1); h1->SetMinimum(0); h1->Draw();
for (Int_t i=0; i<nrEvents; i++) { h2->Fill(r2->Uniform(0,1)); } st->Stop(); cout << "Random2: " << st->CpuTime() << endl; st->Start();
c1->cd(2); h2->SetFillColor(kGreen+1); h2->SetMinimum(0); h2->Draw();
for (Int_t i=0; i<nrEvents; i++) { h3->Fill(r3->Uniform(0,1)); } st->Stop(); cout << "Random3:" << st->CpuTime() << endl;
c1->cd(3);
h3->Draw(); h3->SetFillColor(kBlue+1); h3->SetMinimum(0);
}
This is a another code I am trying to run. But this code doesn't work an it's giving me this error.
warning: Failed to call main() to execute the macro.
Add this function or rename the macro. Falling back to .L.
I tried different things. I tried ,
root [1] .x main.cpp
root [1] .L main.cpp
still giving me same error.
f->SetMarkerColor(1); f->SetMarkerStyle(kOpenStar);) But markers
didn't appear on the graph.
Try f->Draw("PL") instead of f->Draw() to make the markers visible.
warning: Failed to call main() to execute the macro.
Rename your file, it should be called testRandom.cpp instead of main.cpp
Then, you can execute it with .x testRandom.cpp.
First off, I'm aware of the CGAL GIS tutorial, but I just can't seem to copy properties from Point_set to surface mesh.
Any way, I'm loading the LIDAR point cloud to the point set as follows:
using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point = Kernel::Point_3;
using Point_set = CGAL::Point_set_3<Point>;
std::ifstream ifile("input.ply", std::ios_base::binary);
ifile >> point_set;
std::cerr << point_set.size() << " point(s) read" << std::endl;
ifile.close();
I can get the properties via
auto props = point_set.properties();
for (const auto& item : props)
std::cerr << item << std::endl;
// I do know that there exist property "classification" that is of unsigned char type
Point_set::Property_map<unsigned char> original_class_map
= point_set.property_map<unsigned char>("classification").first;
Then, I had tried to set the mesh and had added vertex property, using the code from above mentioned CGAL tutorial. The code below set the point's z coordinate as a property.
auto idx_to_point_with_info
= [&](const Point_set::Index& idx) -> std::pair<Point, Point_set::Index> {
return std::make_pair(point_set.point(idx), idx);
};
using Vbi = CGAL::Triangulation_vertex_base_with_info_2<Point_set::Index, Projection_traits>;
using Fbi = CGAL::Triangulation_face_base_with_info_2<int, Projection_traits>;
using TDS = CGAL::Triangulation_data_structure_2<Vbi, Fbi>;
using TIN_with_info = CGAL::Delaunay_triangulation_2<Projection_traits, TDS>;
TIN_with_info tin_with_info(
boost::make_transform_iterator(point_set.begin(), idx_to_point_with_info),
boost::make_transform_iterator(point_set.end(), idx_to_point_with_info));
auto classification_value = [&](const TIN_with_info::Vertex_handle vh) -> double
{
return vh->point().z();
};
for (TIN_with_info::Vertex_handle vh : tin_with_info.all_vertex_handles())
{ // should work without classification_value, just plain vh->info() = vh->point().z();
vh->info() = classification_value(vh);
}
using Mesh = CGAL::Surface_mesh<Point>;
Mesh tin_class_mesh;
Mesh::Property_map<Mesh::Vertex_index, double> class_map
= tin_class_mesh.add_property_map<Mesh::Vertex_index, double>("v:class").first;
CGAL::copy_face_graph(tin_with_info, tin_class_mesh,
CGAL::parameters::vertex_to_vertex_output_iterator(
boost::make_function_output_iterator(class_lambda)));
std::cerr << tin_class_mesh.number_of_vertices() << " vs " << point_set.size() <<std::endl;
Now, this works just fine, I had successfully set the z coordinate as a property on a mesh.
But, I just can't figure out how can I copy the classification property from the point_set to the tin_class_mesh. I know that I'd need to change double to unsigned char in the code, but I don't know how to access the property from the point_set and assign it to the corresponding vertex in tin_class_mesh. What am I doing wrong?
As a side note, the interesting part here is that the number of tin_colored_mesh.number_of_vertices() differs slightly from the point_set.size(). Why is that?
I saved my training data (maybe float vectors) in some files, and tried to load it as a Tensor using Tensorflow C++ reader class.
Here is my code.
using namespace tensorflow;
using namespace tensorflow::ops;
using namespace tensorflow::sparse;
Scope root = Scope::NewRootScope();
auto indexReader = FixedLengthRecordReader(root, sizeof(uint32_t));
auto queue = FIFOQueue(root, {DataType::DT_STRING});
auto file = Input::Initializer(std::string("mydata.feat"));
std::cerr << file.tensor.DebugString() << std::endl;
auto enqueue = QueueEnqueue(root, queue, {file});
std::cerr << Input(QueueSize(root, queue).size).tensor().DebugString() << std::endl;
auto rawInputIndex = ReaderRead(root, indexReader, queue);
std::cerr << Input(rawInputIndex.key).tensor().DebugString() << std::endl;
auto decodedInputIndex = DecodeRaw(root, rawInputIndex.value, DataType::DT_UINT8);
std::cerr << Input(decodedInputIndex.output).tensor().DebugString() << std::endl;
It is compiled very well but the cerr shows always empty Tensor. (below is execution result of my program on shell)
Tensor<type: string shape: [] values: mydata.feat>
Tensor<type: float shape: [0] values: >
Tensor<type: float shape: [0] values: >
Tensor<type: float shape: [0] values: >
I don't know why it doesn't work.
Or, is there any C++ example code for class ReaderRead or class FIFOQueue? I cannot find it anywhere...
What you're doing is building a graph. To run this graph you need to create a Session and run it. See the label_image example on the tensorflow codebase for an example of how to do this.
I'm running into a problem when trying to use the Z3 optimizer to solve graph partitioning problems. Specifically, the code bellow will fail to produce a satisfying model:
namespace z3 {
expr ite(context& con, expr cond, expr then_, expr else_) {
return to_expr(con, Z3_mk_ite(con, cond, then_, else_));;
}
}
bool smtPart(void) {
// Graph setup
vector<int32_t> nodes = {{ 4, 2, 1, 1 }};
vector<tuple<node_pos_t, node_pos_t, int32_t>> edges;
GraphType graph(nodes, edges);
// Z3 setup
z3::context con;
z3::optimize opt(con);
string n_str = "n", sub_p_str = "_p";
// Re-usable constants
z3::expr zero = con.int_val(0);
// Create the sort representing the different partitions.
const char* part_sort_names[2] = { "P0", "P1" };
z3::func_decl_vector part_consts(con), part_preds(con);
z3::sort part_sort =
con.enumeration_sort("PartID",
2,
part_sort_names,
part_consts,
part_preds);
// Create the constants that represent partition choices.
vector<z3::expr> part_vars;
part_vars.reserve(graph.numNodes());
z3::expr p0_acc = zero,
p1_acc = zero;
typename GraphType::NodeData total_weight = typename GraphType::NodeData();
for (const auto& node : graph.nodes()) {
total_weight += node.data;
ostringstream name;
name << n_str << node.id << sub_p_str;
z3::expr nchoice = con.constant(name.str().c_str(), part_sort);
part_vars.push_back(nchoice);
p0_acc = p0_acc + z3::ite(con,
nchoice == part_consts[0](),
con.int_val(node.data),
zero);
p1_acc = p1_acc + z3::ite(con,
nchoice == part_consts[1](),
con.int_val(node.data),
zero);
}
z3::expr imbalance = con.int_const("imbalance");
opt.add(imbalance ==
z3::ite(con,
p0_acc > p1_acc,
p0_acc - p1_acc,
p1_acc - p0_acc));
z3::expr imbalance_limit = con.real_val(total_weight, 100);
opt.add(imbalance <= imbalance_limit);
z3::expr edge_cut = zero;
for(const auto& edge : graph.edges()) {
edge_cut = edge_cut +
z3::ite(con,
(part_vars[edge.node0().pos()] ==
part_vars[edge.node1().pos()]),
zero,
con.int_val(edge.data));
}
opt.minimize(edge_cut);
opt.minimize(imbalance);
z3::check_result opt_result = opt.check();
if (opt_result == z3::check_result::sat) {
auto mod = opt.get_model();
size_t node_id = 0;
for (z3::expr& npv : part_vars) {
cout << "Node " << node_id++ << ": " << mod.eval(npv) << endl;
}
return true;
} else if (opt_result == z3::check_result::unsat) {
cerr << "Constraints are unsatisfiable." << endl;
return false;
} else {
cerr << "Result is unknown." << endl;
return false;
}
}
If I remove the minimize commands and use a solver instead of an optimize it will find a satisfying model with 0 imbalance. I can also get an optimize to find a satisfying model if I either:
Remove the constraint imbalance <= imbalance_limit or
Make the imbalance limit reducible to an integer. In this example the total weight is 8. If the imbalance limit is set to 8/1, 8/2, 8/4, or 8/8 the optimizer will find satisfying models.
I have tried to_real(imbalance) <= imbalance_limit to no avail. I also considered the possibility that Z3 is using the wrong logic (one that doesn't include theories for real numbers) but I haven't found a way to set that using the C/C++ API.
If anyone could tell me why the optimizer fails in the presence of the real valued constraint or could suggest improvements to my encoding it would be much appreciated. Thanks in advance.
Could you reproduce the result by using opt.to_string() to dump the state (just before the check())? This would create a string formatted in SMT-LIB2 with optimization commands. It is then easier to exchange benchmarks. You should see that it reports unsat with the optimization commands and sat if you comment out the optimization commands.
If you are able to produce a bug, then post an issue on GitHub.com/z3prover/z3.git with a repro.
If not, you can use Z3_open_log before you create the z3 context and record a rerunnable log file. It is possible (but not as easy) to dig into unsoundness bugs that way.
It turns out that this was a bug in Z3. I created an Issue on GitHub and they have since responded with a patch. I'm compiling and testing the fix now, but I expect it to work.
Edit: Yup, that patch fixed the issue for the command line tool and the C++ API.
I have integrated 'Assimp' librairy to load my OBJ/MTL files components.
All works correctly.
But let's have a focus on a following MTL file example:
# Blender MTL File: 'plane.blend'
# Material Count: 1
newmtl PlaneMtl
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Ka ambient_texture.jpg
map_Kd diffuse_texture.jpg
map_Ks specular_texture.jpg
map_Bump bump_texture.jpg
And let's examine the following code:
aiMesh *pMesh = scene->mMeshes[idz];
aiMaterial *pMaterial = scene->mMaterials[pMesh->mMaterialIndex];
aiString ambient_texture_path, diffuse_texture_path, specular_texture_path, bump_texture_path;
pMaterial->GetTexture(aiTextureType_AMBIENT, 0, &ambient_texture_path);
pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &diffuse_texture_path);
pMaterial->GetTexture(aiTextureType_SPECULAR, 0, &specular_texture_path);
pMaterial->GetTexture(aiTextureType_HEIGHT, 0, &bump_texture_path);
std::cout << "AmbientTexture: " << ambient_texture_path.C_Str() << std::endl;
std::cout << "DiffuseTexture: " << diffuse_texture_path.C_Str() << std::endl;
std::cout << "SpecularTexture: " << specular_texture_path.C_Str() << std::endl;
std::cout << "BumpTexture: " << bump_texture_path.C_Str() << std::endl;
Here's the output:
ambient_texture.jpg
diffuse_texture.jpg
specular_texture.jpg
bump_texture.jpg
As you can see all works perfectly and the keywords 'map_Ka, map_Kd, map_Ks and map_Bump' refer to ambient, diffuse, specular and bump (height) map respectively. So these keywords are correct.
But what about normal texture (for normal mapping) and displacement texture (for displacement mapping) for example ?
I tried to add the following lines in my MTL file to test:
map_Normal normal_texture.jpg
map_Disp disp_texture.jpg
using the code:
aiString normal_texture_path, displacement_texture_path;
pMaterial->GetTexture(aiTextureType_NORMALS, 0, &normal_texture_path);
pMaterial->GetTexture(aiTextureType_DISPLACEMENT, 0, &displacement_texture_path);
std::cout << "NormalTexture: " << normal_texture_path.C_Str() << std::endl;
std::cout << "DispTexture: " << displacement_texture_path.C_Str() << std::endl;
and the output:
NormalTexture:
DispTexture:
So the keyword 'map_Normal' and 'map_Disp' are not corrects and so are not part of Wavefront MTL documentation.
I couldn't try to find a correct and official documentation about WaveFront MTL format (only the ones on Wikipedia or tutorials nut nothing official and complete).
Does it exist an official documentation about Wavefront MTL and OBJ format with all the keywords explained within ?
If it's not the case does anyone knows the keywords for normal and displacement texture ?
I know this is an old question, but I needed to use normal maps, and I'm using Assimp and OBJ files, so I searched and found the answer to this. After looking at the source code of Assimp you can see in assimp/code/ObjFileMtlImporter.cpp:
static const std::string DiffuseTexture = "map_Kd";
static const std::string AmbientTexture = "map_Ka";
static const std::string SpecularTexture = "map_Ks";
static const std::string OpacityTexture = "map_d";
static const std::string EmissiveTexture = "map_emissive";
static const std::string EmissiveTexture_1 = "map_Ke";
static const std::string BumpTexture1 = "map_bump";
static const std::string BumpTexture2 = "map_Bump";
static const std::string BumpTexture3 = "bump";
static const std::string NormalTexture = "map_Kn";
static const std::string ReflectionTexture = "refl";
static const std::string DisplacementTexture = "disp";
static const std::string SpecularityTexture = "map_ns";
So as you can see, Assimp use map_Kn to refer to normal textures anddisp for displacement textures (confirmed when I loaded a model after modifying its MTL).
Alias/Wavefront is now so old and passed through so many owning companies that I very much doubt you will find an 'official' specification anywhere.
I suggest the excellent write-up by Paul Bourke, which includes the keywords details for bump and displacement maps. (But not normal maps - I don't think they were ever official for OBJ)
http://paulbourke.net/dataformats/mtl/
Hope this helps.