Discussion:
[brlcad-devel] Handling OBJ files
Gauravjeet Singh
2016-07-09 05:27:41 UTC
Permalink
I have been reading about OBJ files recently.
The problem in OGV is that there are too many obj files for a single .g file.

The ill effect of this is that it increases the number of HTTP
requests while loading a model.
For a simple model, almost 100 obj files are created, thus 100 HTTP requests.

There are two methods to tackle this:
One is to merge the different obj files into a single obj file while viewing.
Another is to convert a .g file into obj file in such a way that the
number of obj files created is comparatively less.

So is there any command in BRL-CAD that can hack the merge of several
different obj files.
Or Is there any parameter of g-obj command that will reduce the number
of obj files created for a single .g file.
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh
Gauravjeet Singh
2016-07-09 05:39:53 UTC
Permalink
On Sat, Jul 9, 2016 at 10:57 AM, Gauravjeet Singh
<***@gmail.com> wrote:
<snip>
Post by Gauravjeet Singh
So is there any command in BRL-CAD that can hack the merge of several
different obj files.
<snip>

I read the format of an obj file, and if we try to manually merge two
obj files, it shall be done as following:

https://gist.github.com/Gauravjeetsingh/19444ad7059952e7d0058f6fa7f8365d

But manually doing each file will take a lot of time in OGV, and will
be erroneous.
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh
Gauravjeet Singh
2016-07-14 07:07:10 UTC
Permalink
On Sat, Jul 9, 2016 at 10:57 AM, Gauravjeet Singh
Post by Gauravjeet Singh
So is there any command in BRL-CAD that can hack the merge of several
different obj files.
Or Is there any parameter of g-obj command that will reduce the number
of obj files created for a single .g file.
Obj file is created for each object in mged's .g file.
For instance, I have a model axis.g

if I do
$ mged axis.g ls -a

I will get the following
X X.r X1 X2 Y Y.r Y1 Y2 Z Z.r Z1 Z2 Z3 _GLOBAL all arb5 arb7 arb8 axis
ellg tgc tor x x.r xx xx.r y y.r yy yy.r z z.r zz zz.r

To reduce the no of files, one thing we can do is to merge objects. I
read the documentation of mged, and came to know about r command. if
we do the following in mged's console

$ r new u X u X1 u X2 u Y u Y1 u Y2 u Z u Z1 u Z2 u Z3 u all u
........ and so on
we get an object new, the obj file of this object will now compose of
an entire model.

I rendered just the new.obj file, and amazingly it rendered the complete model.
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh
Inderpreet Singh
2016-07-17 17:56:22 UTC
Permalink
On Thu, Jul 14, 2016 at 12:37 PM, Gauravjeet Singh
Post by Gauravjeet Singh
I rendered just the new.obj file, and amazingly it rendered the complete model.
Bravo! that would really optimize OGV.

I understood the implementation at mged level but yet can't see any
code related to it. I am curious to know about your implementation.

--
Inderpreet Singh

Ekoankar Sahai
ishwerdas.com
facebook.com/okayinder
https://kippt.com/okayinder
Gauravjeet Singh
2016-07-24 07:23:53 UTC
Permalink
Post by Inderpreet Singh
I understood the implementation at mged level but yet can't see any
code related to it. I am curious to know about your implementation.
Hello Inder,

Regarding implementation, here's what I have doing this till now

I found the file where mged and g-obj commands are being executed from
OGV (server/cfs_uploader.js)

The workflow is something like this

Step 1: Execute mged Ls command to get a list of objects
Filter the objects without .r extension in their name using a regular
expression.
(The regular expression that works here is /\w*\.r/g )

Step 2: Merge the name of objects using a for loop in the final merge
command, which looks something like following.
(mged filepath r newObjName u obj1 u obj 2 ....)

Step 3: Execute merge command

Step 4: Execute mged ls command once again to get list of objects
Use for loop to convert each object to its respective obj file.

The problem, However, I am having with this is
It doesn't execute in this sequence. Step 4 executes first and Step 3
afterwards. This makes the merged object in .g file, but its
respective obj file isn't generated.
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh
Gauravjeet Singh
2016-08-03 10:51:28 UTC
Permalink
On Sun, Jul 24, 2016 at 12:53 PM, Gauravjeet Singh
Post by Gauravjeet Singh
The problem, However, I am having with this is
It doesn't execute in this sequence. Step 4 executes first and Step 3
afterwards. This makes the merged object in .g file, but its
respective obj file isn't generated.
Hello,
The problem I mentioned above is solved.

Another problem I face is model specific. There's a .g file named
axis.g in BRL-CAD examples. It works perfectly fine. But when I upload
another model, for instance ktang.g (also in BRL-CAD examples), I get
the following error on converting the merged object to .obj file.

"aagWadraD9338g3fH" is the name of merged object in the following log.


nmg_bool(): Dangling faces detected in rA after boolean
I20160803-15:22:07.209(5.5)?
I20160803-15:22:07.210(5.5)? conversion of /aagWadraD9338g3fH FAILED!
I20160803-15:22:07.211(5.5)?
I20160803-15:22:07.212(5.5)? ERROR: bad pointer 0xad5180: s/b union
tree(x91191191), was Zero_Magic_Number(x0), file
/home/jordi/Escriptori/brlcad-7.24.2/src/librt/db_tree.c, line 1339

Any leads how to solve this ?
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh

------------------------------------------------------------------------------
Gauravjeet Singh
2016-08-03 10:56:13 UTC
Permalink
On Wed, Aug 3, 2016 at 4:21 PM, Gauravjeet Singh
Post by Gauravjeet Singh
nmg_bool(): Dangling faces detected in rA after boolean
I20160803-15:22:07.209(5.5)?
I20160803-15:22:07.210(5.5)? conversion of /aagWadraD9338g3fH FAILED!
I20160803-15:22:07.211(5.5)?
I20160803-15:22:07.212(5.5)? ERROR: bad pointer 0xad5180: s/b union
tree(x91191191), was Zero_Magic_Number(x0), file
/home/jordi/Escriptori/brlcad-7.24.2/src/librt/db_tree.c, line 1339
Complete log
https://gist.githubusercontent.com/Gauravjeetsingh/b39960e3440e83dcf9bfc0583ab206d6/raw/92b6dae910cbec592484e435e7bfa461dc1da1e8/conversion%2520log
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh

------------------------------------------------------------------------------
Christopher Sean Morrison
2016-08-03 13:15:46 UTC
Permalink
Post by Gauravjeet Singh
The problem I mentioned above is solved.
Please be more specific so others can learn. What exactly was wrong and how did you fix it?
Post by Gauravjeet Singh
Another problem I face is model specific. There's a .g file named
axis.g in BRL-CAD examples. It works perfectly fine. But when I upload
another model, for instance ktang.g (also in BRL-CAD examples), I get
the following error on converting the merged object to .obj file.
Is ktank merged with axis or are the regions in ktank being merged together?
Post by Gauravjeet Singh
nmg_bool(): Dangling faces detected in rA after boolean
I20160803-15:22:07.209(5.5)?
I20160803-15:22:07.210(5.5)? conversion of /aagWadraD9338g3fH FAILED!
I20160803-15:22:07.211(5.5)?
I20160803-15:22:07.212(5.5)? ERROR: bad pointer 0xad5180: s/b union
tree(x91191191), was Zero_Magic_Number(x0), file
/home/jordi/Escriptori/brlcad-7.24.2/src/librt/db_tree.c, line 1339
Any leads how to solve this ?
This is not easy to solve robustly because it’s logically NP-complete and numerically unstable due to floating point. Basically, it’s trying to turn the implicit CSG boolean expression into a single triangle mesh. It’s failing.

This can happen for many many reasons. In this particular case, it looks like it’s failing because of a logic bug. At some point in the processing, it is expecting to get a tree of geometry and it encountered a NULL pointer instead. Instead of crashing, it detected the situation and aborted the boolean evaluation.

So, options. You can debug the logic bug, figure out where the NULL came from or make it do something besides abort when encountered — this requires proficiency with a debugger and C. You can often get a successful conversion by “nudging” the evaluation down a different logic path — this basically entails trying out different tolerance values like -a 0.1(brlman g-obj) when a failure is encountered. You could change the backend to avoid traditional boolean evaluation — this would be a major architecture change involving something like verbnurbs or csg.js or

Cheers!
Sean


------------------------------------------------------------------------------
Christopher Sean Morrison
2016-08-03 13:18:13 UTC
Permalink
Post by Christopher Sean Morrison
This is not easy to solve robustly because it’s logically NP-complete and numerically unstable due to floating point. Basically, it’s trying to turn the implicit CSG boolean expression into a single triangle mesh. It’s failing.
For anyone looking for more background information on this, Check wrote up a decent proposal to research the topic in more depth last year:

https://brlcad.org/wiki/User:NyahCheck/Survey_of_CSG_Algorithms

There’s a few other options not listed there, but it’s a good start and would make for a good future project for someone.

Cheers!
Sean


------------------------------------------------------------------------------
Christopher Sean Morrison
2016-08-03 12:57:08 UTC
Permalink
Post by Gauravjeet Singh
Step 1: Execute mged Ls command to get a list of objects
Filter the objects without .r extension in their name using a regular
expression.
(The regular expression that works here is /\w*\.r/g )
You shouldn’t make any assumptions about how objects are named. If you want objects that are or are not regions, the “search” command will be more robust (see the "-type region” option, run “brlman search” for docs).
Post by Gauravjeet Singh
Step 2: Merge the name of objects using a for loop in the final merge
command, which looks something like following.
(mged filepath r newObjName u obj1 u obj 2 ….)
Why are you doing this? This presumably changes the model into a single region (i.e., a single part) and also potentially changes the definition/shape of the geometry (depending on how the regions were arranged). I don’t see this being beneficial.
Post by Gauravjeet Singh
Step 3: Execute merge command
Step 4: Execute mged ls command once again to get list of objects
Use for loop to convert each object to its respective obj file.
The problem, However, I am having with this is
It doesn't execute in this sequence. Step 4 executes first and Step 3
afterwards. This makes the merged object in .g file, but its
respective obj file isn't generated.
To figure out what processing is needed, have to understand what you’re trying to accomplish. This is not clear to me. Is the goal to have one .obj file per part (regions), one .obj per assembly (combs above region), one per .g file?

Cheers!
Sean


------------------------------------------------------------------------------
Gauravjeet Singh
2016-08-06 10:00:31 UTC
Permalink
On Wed, Aug 3, 2016 at 6:27 PM, Christopher Sean Morrison
Post by Christopher Sean Morrison
You shouldn’t make any assumptions about how objects are named. If you want objects that are or are not regions, the “search” command will be more robust (see the "-type region” option, run “brlman search” for docs).
Ah! Alright.
This sure seems a better approach.
I did the following command
search -not -type region
to get all the objects which aren't a region.
Post by Christopher Sean Morrison
Why are you doing this? This presumably changes the model into a single region (i.e., a single part) and also potentially changes the definition/shape of the geometry (depending on how the regions were arranged). I don’t see this being beneficial.
I tried doing it with model axis.g and there was no change in shape.
So thought maybe it would work with each model.
What I wanted to do was to get a mged object which includes the entire
model, and therefore after conversion we may get an obj file that
renders the entire model.
Post by Christopher Sean Morrison
To figure out what processing is needed, have to understand what you’re trying to accomplish. This is not clear to me. Is the goal to have one .obj file per part (regions), one .obj per assembly (combs above region), one per .g file?
Our goal is to reduce the number of .obj files per model. And if
possible keep it to one per model for viewing purpose.
For further tweaking and analyzing like giving a different color to
each part of the model, we can render another version of the model
that has different obj file for different part of the model.
--
Fear is wisdom in the face of danger. It’s nothing to be ashamed of

Gauravjeet Singh
http://github.com/gauravjeetsingh

------------------------------------------------------------------------------
Inderpreet Singh
2016-08-06 10:07:51 UTC
Permalink
On Sat, Aug 6, 2016 at 3:30 PM, Gauravjeet Singh
Post by Gauravjeet Singh
Our goal is to reduce the number of .obj files per model. And if
possible keep it to one per model for viewing purpose.
For further tweaking and analyzing like giving a different color to
each part of the model, we can render another version of the model
that has different obj file for different part of the model.
Welcome back Gaurav,

I had taken a look at your merge code before you removed it yesterday
with the new commit. Looking at the timeline and amount of work left I
would suggest you to make a new branch with this merge code and
improve on it as you understand more about how mged works. Keep
deployment at your top-most priority and then work on other tasks left
side by side. Also keep updating your logs.

--
Inderpreet Singh

Ekoankar Sahai
ishwerdas.com
facebook.com/okayinder
https://kippt.com/okayinder

------------------------------------------------------------------------------
Inderpreet Singh
2016-08-06 10:11:28 UTC
Permalink
Post by Inderpreet Singh
Keep
deployment at your top-most priority and then work on other tasks left
side by side.
If you are facing any problem in deployment, feel free to ask.

--
Inderpreet Singh

Ekoankar Sahai
ishwerdas.com
facebook.com/okayinder
https://kippt.com/okayinder

------------------------------------------------------------------------------
Christopher Sean Morrison
2016-08-06 12:33:59 UTC
Permalink
Post by Gauravjeet Singh
Ah! Alright.
This sure seems a better approach.
I did the following command
search -not -type region
to get all the objects which aren't a region.
I don’t think this is what you probably want either. There are objects above regions (combs that are assemblies/groups) and objects below regions (combs that are shapes and primitives). Processing tools usually want either objects above region, regions, or objects below regions, but I’ve never seen a need for “not regions”.
Post by Gauravjeet Singh
I tried doing it with model axis.g and there was no change in shape.
So thought maybe it would work with each model.
What I wanted to do was to get a mged object which includes the entire
model, and therefore after conversion we may get an obj file that
renders the entire model.
A single object that represents everything or a set of objects that represents everything in the model? For the prior, that would be each top-level objects (run the “tops” command). For the latter, that would be all regions.
Post by Gauravjeet Singh
Our goal is to reduce the number of .obj files per model. And if
possible keep it to one per model for viewing purpose.
For further tweaking and analyzing like giving a different color to
each part of the model, we can render another version of the model
that has different obj file for different part of the model.
Then it sounds like you need both, but the “tops” listing may be what you’re looking for. That’s the starting point on all models — save each top-level out as an obj. Note that doing this will only success approximately 90% of the time for any given object, so you will want some fallback or error handling code.

Cheers!
Sean


------------------------------------------------------------------------------
Continue reading on narkive:
Loading...