Skip to content

Commit e88b47a

Browse files
committed
testPlugin2.1
Separate each plugin in its separate file, compile together (with -O3 size file is reduced from 8.6 MB to 1.1 MB)
1 parent e87de8c commit e88b47a

File tree

16 files changed

+835
-14881
lines changed

16 files changed

+835
-14881
lines changed

test/testPlugin2.1/MeshLabJs.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdlib.h>
2+
#include <emscripten.h>
3+
#include <emscripten/bind.h>
4+
#include <vcg/complex/complex.h>
5+
6+
using namespace vcg;
7+
using namespace std;
8+
using namespace emscripten;
9+
10+
class MyVertex; class MyEdge; class MyFace;
11+
struct MyUsedTypes : public vcg::UsedTypes<vcg::Use<MyVertex> ::AsVertexType,
12+
vcg::Use<MyEdge> ::AsEdgeType,
13+
vcg::Use<MyFace> ::AsFaceType>{};
14+
15+
class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
16+
class MyFace : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::VertexRef, vcg::face::BitFlags > {};
17+
class MyEdge : public vcg::Edge< MyUsedTypes> {};
18+
19+
class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
20+
21+
class MyVertex0 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::BitFlags >{};
22+
class MyVertex1 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
23+
class MyVertex2 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Color4b, vcg::vertex::CurvatureDirf,
24+
vcg::vertex::Qualityf, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
25+
26+
class MeshLabJs {
27+
28+
// private:
29+
// string fileName;
30+
31+
public:
32+
MyMesh *m;
33+
MeshLabJs(uintptr_t _m){
34+
m = (MyMesh*) _m;
35+
}
36+
37+
int getVertexNumber(){ return m->VN(); }
38+
39+
inline uintptr_t getVertexVector() {
40+
float * v = new float[m->VN()*3];
41+
int k=0;
42+
for (int i = 0; i < m->VN(); i++){
43+
for (int j = 0; j < 3; j++){
44+
v[k] = m->vert[i].cP()[j];
45+
k++;
46+
}
47+
}
48+
return (uintptr_t)v;
49+
}
50+
51+
inline int getFaceNumber() { return m->FN(); }
52+
53+
inline uintptr_t getFaceVector() {
54+
int * f = new int[m->FN()*3];
55+
int k=0;
56+
for (int i = 0; i < m->FN(); i++)
57+
for (int j = 0; j < 3; j++){
58+
f[k] = (int)tri::Index(*m,m->face[i].cV(j));
59+
k++;
60+
}
61+
return (uintptr_t)f;
62+
}
63+
};
64+
65+
EMSCRIPTEN_BINDINGS(MeshLabJs) {
66+
class_<MeshLabJs>("MeshLabJs")
67+
.constructor<uintptr_t>()
68+
.function("getVertexNumber", &MeshLabJs::getVertexNumber)
69+
.function("getFaceNumber", &MeshLabJs::getFaceNumber)
70+
.function("getFaceVector", &MeshLabJs::getFaceVector)
71+
.function("getVertexVector", &MeshLabJs::getVertexVector)
72+
;
73+
}

test/testPlugin2.1/MeshlabJs.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/testPlugin2.1/MeshlabJs.js.mem

35.3 KB
Binary file not shown.

test/testPlugin2.1/Opener.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <stdlib.h>
2+
#include <emscripten.h>
3+
#include <emscripten/bind.h>
4+
#include <vcg/complex/complex.h>
5+
#include <wrap/io_trimesh/import.h>
6+
7+
using namespace vcg;
8+
using namespace std;
9+
using namespace emscripten;
10+
11+
class MyVertex; class MyEdge; class MyFace;
12+
struct MyUsedTypes : public vcg::UsedTypes<vcg::Use<MyVertex> ::AsVertexType,
13+
vcg::Use<MyEdge> ::AsEdgeType,
14+
vcg::Use<MyFace> ::AsFaceType>{};
15+
16+
class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
17+
class MyFace : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::VertexRef, vcg::face::BitFlags > {};
18+
class MyEdge : public vcg::Edge< MyUsedTypes> {};
19+
20+
class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
21+
22+
class MyVertex0 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::BitFlags >{};
23+
class MyVertex1 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
24+
class MyVertex2 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Color4b, vcg::vertex::CurvatureDirf,
25+
vcg::vertex::Qualityf, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
26+
27+
class Opener
28+
{
29+
public:
30+
MyMesh m;
31+
int openMesh(string fileName) {
32+
int loadmask;
33+
int ret=vcg::tri::io::Importer<MyMesh>::Open(m,fileName.c_str(),loadmask);
34+
if(ret!=0) {
35+
printf("Error in opening file\n");
36+
exit(-1);
37+
}
38+
printf("Read mesh %i %i\n",m.FN(),m.VN());
39+
return ret;
40+
}
41+
42+
uintptr_t getMesh(){
43+
return (uintptr_t)((void*)(&m)) ;
44+
}
45+
46+
};
47+
48+
//Binding code
49+
EMSCRIPTEN_BINDINGS(Opener) {
50+
class_<Opener>("Opener")
51+
.constructor<>()
52+
.function("openMesh", &Opener::openMesh)
53+
.function("getMesh", &Opener::getMesh)
54+
;
55+
}

test/testPlugin2.1/Refine.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdlib.h>
2+
#include <emscripten.h>
3+
#include <emscripten/bind.h>
4+
#include <vcg/complex/complex.h>
5+
#include <vcg/complex/algorithms/update/topology.h>
6+
#include <vcg/complex/algorithms/update/normal.h>
7+
#include <vcg/complex/algorithms/update/flag.h>
8+
#include <vcg/complex/algorithms/refine.h>
9+
#include <vcg/complex/algorithms/refine_loop.h>
10+
11+
12+
using namespace vcg;
13+
using namespace std;
14+
using namespace emscripten;
15+
16+
class MyVertex; class MyEdge; class MyFace;
17+
struct MyUsedTypes : public vcg::UsedTypes<vcg::Use<MyVertex> ::AsVertexType,
18+
vcg::Use<MyEdge> ::AsEdgeType,
19+
vcg::Use<MyFace> ::AsFaceType>{};
20+
21+
class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
22+
class MyFace : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::VertexRef, vcg::face::BitFlags > {};
23+
class MyEdge : public vcg::Edge< MyUsedTypes> {};
24+
25+
class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
26+
27+
class MyVertex0 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::BitFlags >{};
28+
class MyVertex1 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
29+
class MyVertex2 : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Color4b, vcg::vertex::CurvatureDirf,
30+
vcg::vertex::Qualityf, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
31+
32+
class MyRefine {
33+
34+
public:
35+
36+
MyMesh *m;
37+
MyRefine(uintptr_t _m){
38+
m = (MyMesh*) _m;
39+
}
40+
void refinement(int step)
41+
{
42+
int t2=clock();
43+
tri::UpdateTopology<MyMesh>::FaceFace(*m);
44+
tri::EdgeLen<MyMesh,float> edgePred(0);
45+
tri::MidPoint<MyMesh> midFun(m);
46+
for(int i=0;i<step;i++)
47+
tri::RefineE(*m,midFun,edgePred);
48+
int t3=clock();
49+
printf("Refined mesh %i vert - %i face \n",m->VN(),m->FN());
50+
printf("Refinement time %5.2f\n",float(t3-t2)/CLOCKS_PER_SEC);
51+
}
52+
};
53+
54+
//Binding code
55+
EMSCRIPTEN_BINDINGS(MyRefine) {
56+
class_<MyRefine>("MyRefine")
57+
.constructor<uintptr_t>()
58+
.function("myRefine", &MyRefine::refinement)
59+
;
60+
}

test/testPlugin2.1/build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#issue: cannot compile many sources with an header.....
2+
#with -O3 8.6 MB -> 1.1 MB
3+
emcc --bind -s DEMANGLE_SUPPORT=1 -I ../../../vcglib/ MeshLabJs.cpp Opener.cpp Refine.cpp ../../../vcglib/wrap/ply/plylib.cpp -s TOTAL_MEMORY=268435456 -O3 -o MeshlabJs.js
4+
5+
6+
#with split
7+
# EMCC_FAST_COMPILER=0 emcc --bind --split 1024000 -s DEMANGLE_SUPPORT=1 -I ../../../vcglib/ open.cpp ../../../vcglib/wrap/ply/plylib.cpp -s TOTAL_MEMORY=268435456 -o open.js
8+

test/testPlugin2.1/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<title>testPlugin</title>
7+
<style>
8+
body { margin: 0; }
9+
.cont {
10+
width: 850px;
11+
max-width: 100%;
12+
min-height: 100%;
13+
height: 450px;
14+
margin: 0 auto -30px;
15+
/*width:450px;*/
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<script src="js/engine/Module.js"></script>
21+
<input type="file" id="files" name="files[]" value="tetra.off" />
22+
<br>
23+
<output id="list"></output>
24+
<textarea id="output" rows="8"></textarea>
25+
<form>
26+
Mesh name:<br>
27+
<input type="text" id="name_mesh" name="mesh_name" value="mesh.off">
28+
<br>
29+
</form>
30+
<button id='save_mesh' style="display:none;">Save Mesh</button>
31+
<button id='refinement'>Refine!</button>
32+
<div id='renderingMesh' class='cont'> </div>
33+
</body>
34+
<script src="../js/three.js"></script>
35+
<script src="../js/controls/TrackballControls.js"></script>
36+
<script src="MeshlabJs.js"></script>
37+
<script src="js/engine/Rendering.js"></script>
38+
<script src="js/engine/Engine.js"> </script>
39+
</html>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var camera, scene, renderer, controls, time, mesh, ptrMesh;
2+
//declaration of plugin global variable
3+
var Refine;
4+
5+
init();
6+
7+
8+
function handleFileSelect(evt) {
9+
var files = evt.target.files; // FileList object
10+
console.log("Name: ", files[0].name);
11+
console.log("Size: ", files[0].size);
12+
console.time("File Reading Time");
13+
14+
//extract format file
15+
var fileName = files[0].name;
16+
var format = fileName.split(".");
17+
format = format[format.length-1];
18+
fileName= "tmp." + format;
19+
switch(format){
20+
case "off": {break;}
21+
case "obj": {break;}
22+
case "ply": {break;}
23+
case "stl": {break;}
24+
case "vmi": {break;}
25+
default : {
26+
alert("MeshLabJs allows file format '.off', '.ply', '.vmi', '.obj' and '.stl'. \nTry again.")
27+
return;
28+
}
29+
}
30+
31+
var fileToLoad = files[0];
32+
var fileReader = new FileReader();
33+
34+
fileReader.onload = function (fileLoadedEvent) {
35+
36+
// Emscripten need a Arrayview so from the returned arraybuffer we must create a view of it as 8bit chars
37+
var int8buf = new Int8Array(fileLoadedEvent.target.result);
38+
FS.createDataFile("/", fileName, int8buf, true, true);
39+
40+
console.log("Read file", fileLoadedEvent.target.result.byteLength );
41+
console.timeEnd("File Reading Time");
42+
43+
console.time("Parsing mesh Time");
44+
var Opener = new Module.Opener();
45+
var resOpen = Opener.openMesh(fileName);
46+
console.log("Open mesh with result "+resOpen);
47+
console.timeEnd("Parsing mesh Time");
48+
ptrMesh = Opener.getMesh();
49+
50+
createMesh(ptrMesh);
51+
animate();
52+
53+
FS.unlink(fileName);
54+
};
55+
56+
fileReader.readAsArrayBuffer(fileToLoad, "UTF-8"); // Efficient binary read.
57+
}
58+
document.getElementById('files').addEventListener('change', handleFileSelect, false);
59+
60+
61+
//handler for plugin REFINE
62+
document.getElementById('refinement').addEventListener('click', function refineMesh(){
63+
console.time("Refine time ");
64+
Refine = new Module.MyRefine(ptrMesh);
65+
Refine.myRefine(1);
66+
console.timeEnd("Refine time ");
67+
console.time("Update mesh ");
68+
createMesh(ptrMesh);
69+
console.timeEnd("Update mesh ");
70+
});
71+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Module is a global JavaScript object with attributes that
3+
Emscripten-generated code calls at various points in its execution.
4+
5+
Developers can provide an implementation of Module to control
6+
the execution of code. For example, to define how notification
7+
messages from Emscripten are displayed, developers implement the
8+
Module.print attribute.
9+
*/
10+
var Module = {
11+
preRun: [],
12+
postRun: [],
13+
print: (function () {
14+
var element = document.getElementById('output');
15+
if (element) { element.value = ''; }// clear browser cache
16+
return function (text) {
17+
text = Array.prototype.slice.call(arguments).join(' ');
18+
console.log(text);
19+
if (element) {
20+
element.value += text + "\n";
21+
element.scrollTop = element.scrollHeight; // focus on bottom
22+
}
23+
};
24+
})()
25+
};

0 commit comments

Comments
 (0)