CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Ray Tracing in OpenFOAM

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 4 Post By mirx
  • 2 Post By mirx

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 11, 2013, 12:29
Default Ray Tracing in OpenFOAM
  #1
New Member
 
Matteo
Join Date: Sep 2013
Location: Milan, Italy
Posts: 9
Rep Power: 13
MMC15 is on a distinguished road
Hi OpenFOAMers!!

I'm looking for an algorithm to search the intersection between a line and cell faces of a mesh. I know Ray Tracing, which is quite similar to my problem, is an important field of study for graphic programmers, but I didn't find nothing practically interesting about CFD mesh applications. I've already searched in the forum but I didn't find nothing.

Could anybody help me?
Thank you a lot!

Regards from Italy!
MMC15 is offline   Reply With Quote

Old   December 11, 2013, 14:05
Default
  #2
New Member
 
Michael
Join Date: Sep 2012
Posts: 23
Rep Power: 14
mirx is on a distinguished road
There are a lot of functions in OF to find intersections of faces and lines/rays. Usually they are methods of indexedOctree classes. It only depends on, what you would like to intersect.
Boundary faces, triSurface faces, mesh faces?
With lines (find intersection between start point and end point) or with rays (start point and vector)?

Do you just need to find these intersections or are you really looking for a ray tracing algorithm, like monte carlo ray tracing?
mirx is offline   Reply With Quote

Old   December 12, 2013, 07:53
Default
  #3
New Member
 
Matteo
Join Date: Sep 2013
Location: Milan, Italy
Posts: 9
Rep Power: 13
MMC15 is on a distinguished road
I'd like to know the intersection points of a line from a start point to an end point with every mesh faces at runtime.
The indexedOctree functions may be useful, but I'm having troubles with their use.
Have you some advices about their application?
Which headers have I to include?
Is there a specific function that you suggest?
MMC15 is offline   Reply With Quote

Old   December 12, 2013, 08:11
Default
  #4
New Member
 
Michael
Join Date: Sep 2012
Posts: 23
Rep Power: 14
mirx is on a distinguished road
Here are some code snippets I used to find all intersections of a line with all mesh faces. This is no complete working code.

Code:
#include "polyMesh.H"
#include "meshTools.H"
#include "treeDataFace.H"
Code:
    treeBoundBox allBb(mesh.points());


    scalar bbTol = 1e-6 * allBb.avgDim();


    point& bbMin = allBb.min();
    bbMin.x() -= bbTol;
    bbMin.y() -= bbTol;
    bbMin.z() -= bbTol;


    point& bbMax = allBb.max();
    bbMax.x() += 2*bbTol;
    bbMax.y() += 2*bbTol;
    bbMax.z() += 2*bbTol;


    indexedOctree<treeDataFace> faceTree
    (
        treeDataFace(false, mesh_),
        allBb, // overall search domain
        8, // maxLevel
        10, // leafsize
        3.0 // duplicity
    );
Code:
        const point pStart = ...; // start point of line

        const point pEnd = ...; // end point of line

        const vector eVec(pEnd - pStart); // line vector

        const scalar eMag = mag(eVec); // edge length
        const vector tolVec = 1e-6*eVec;        
        point p0 = pStart - tolVec;
        const point p1 = pEnd + tolVec;
        


        while(true)
        {
            pointIndexHit pHit = faceTree.findLine(p0, p1);
            
            if (pHit.hit())
            {
                const label faceI = pHit.index(); // face label of hit face

                const point hitPoint = pHit.hitPoint(); // intersection point

                

                const vector& area = mesh_.faceAreas()[pHit.index()];                scalar typDim = Foam::sqrt(mag(area));
                
                // stop search if new start point is near to edge end

                if ((mag(pHit.hitPoint() - pEnd)/typDim) < SMALL)
                {
                    break;
                }

                // set new start point shortly after previous start point

                p0 = pHit.hitPoint() + tolVec;
            }
            else
            {
                // No hit.
                break;
            }
        }
EDIT: The while loop is used to find ALL intersections. The faceTree.findLine(p0, p1) method only returns the first intersection starting from p0. To find all intersections you have to change the starting point, if findLine() found an intersection.
karaul, MMC15, jacksonxing and 1 others like this.
mirx is offline   Reply With Quote

Old   December 12, 2013, 09:54
Default
  #5
New Member
 
Matteo
Join Date: Sep 2013
Location: Milan, Italy
Posts: 9
Rep Power: 13
MMC15 is on a distinguished road
Thank you mirx.
Nice code, it works very well, I just add a list to save the hit points.

Code:
DynamicList<pointIndexHit> pHitList;
Code:
pHitList.append(pHit);
I read that exists an other indexedOctree function called findLineAny, is there a reason why don't you use it, instead of findLine?
MMC15 is offline   Reply With Quote

Old   December 12, 2013, 10:03
Default
  #6
New Member
 
Michael
Join Date: Sep 2012
Posts: 23
Rep Power: 14
mirx is on a distinguished road
There was a reason that I didn't use it. I wanted to run some extra code for every intersection I had found.

But you are right. findLineAny actually executes a similar while loop as the one I have posted. I think my loop was inspired by the one in findLineAny. So if you just need the intersections, I think you can use something like:

Code:
const point pStart = ...; 
const point pEnd = ...; 
List<pointIndexHit> pHit(faceTree.findLineAny(pStart, pEnd));
mirx is offline   Reply With Quote

Old   December 12, 2013, 10:27
Default
  #7
New Member
 
Matteo
Join Date: Sep 2013
Location: Milan, Italy
Posts: 9
Rep Power: 13
MMC15 is on a distinguished road
I tried that but the list output seems to be not accepted:
Code:
 error: no matching function for call to ‘Foam::List<Foam::PointIndexHit<Foam::Vector<double> > >::List(Foam::pointIndexHit)’
     List<pointIndexHit> pHit(faceTree.findLineAny(pStart, pEnd));’
MMC15 is offline   Reply With Quote

Old   December 12, 2013, 10:36
Default
  #8
New Member
 
Michael
Join Date: Sep 2012
Posts: 23
Rep Power: 14
mirx is on a distinguished road
Oh sorry, my mistake. I confused Any with All. triSurfaceSearch has a method called fineLineAll.
Code:
void findLineAll        
(
     const point& start,
     const point& end,
     List<pointIndexHit>&
) const;
This returns a list of all pointIndexHit. indexedOctree doesn't have this method. (Of course... triSurfaceSearch.findLineAll() uses indexedOctree's findLine function with a loop)

findLineAny returns any of the intersections, not all.
mirx is offline   Reply With Quote

Old   December 12, 2013, 10:42
Default
  #9
New Member
 
Matteo
Join Date: Sep 2013
Location: Milan, Italy
Posts: 9
Rep Power: 13
MMC15 is on a distinguished road
OK! Thank you a lot for your useful replies!

Bye!

P.S.: I said "Any", so it was my mistake! :-)
MMC15 is offline   Reply With Quote

Old   May 16, 2014, 13:38
Default including header files
  #10
New Member
 
Join Date: Apr 2012
Posts: 21
Rep Power: 14
Yahoo is on a distinguished road
Hey guys,

Is any body still interested in this ray tracing application in OpenFOAM?

I am having trouble with including the header file "meshtools.H"--fatal error, no such file or directory.

Do you have any comments?
Yahoo is offline   Reply With Quote

Old   May 16, 2014, 14:04
Default
  #11
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

it's meshTools.H not meshtools.h. Also you need

Code:
-I$(LIB_SRC)/meshTools/lnInclude
in your options file.
alexeym is offline   Reply With Quote

Old   May 27, 2014, 18:04
Default
  #12
New Member
 
Join Date: Apr 2012
Posts: 21
Rep Power: 14
Yahoo is on a distinguished road
Thanks Alexeym!
I got it going!
one more question: do you know how can i use findLineAll member function of triSurfaceSearch?
Yahoo is offline   Reply With Quote

Old   May 28, 2014, 04:01
Default
  #13
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

Looked through the documentation on the method, now I have doubts I've got your question right. You've got triSurface, you construct triSurfaceSearch, you call findLineAll. So what's your question?
alexeym is offline   Reply With Quote

Old   May 28, 2014, 04:32
Default
  #14
New Member
 
Michael
Join Date: Sep 2012
Posts: 23
Rep Power: 14
mirx is on a distinguished road
For a ray tracing application I think you will be interested in the first intersection of a ray with a surface. In this case you may use method findLine of indexedOctree<treeDataTriSurface>.

This is a short example how to use this method:

Code:
const triSurface surf(runTime.constantPath()/"triSurface"/surfFileName);
const vectorField& normals = surf.faceNormals();
const triSurfaceSearch querySurf(surf);
const indexedOctree<treeDataTriSurface>& tree = querySurf.tree();
point searchStart(0, 0, 0);
point searchEnd(1, 0, 0);
pointIndexHit pHit = tree.findLine(searchStart, searchEnd);
if ( pHit.hit() )
{
    point hitPoint = pHit.hitPoint();
    label hitTriangle = pHit.index();
    vector normal = normals[hitTriangle];
}
With the triangle label you are able to identify the plane normal and thus the interaction of the ray with the surface (reflection, refraction).
David* and karaul like this.
mirx is offline   Reply With Quote

Old   February 22, 2015, 07:00
Default
  #15
New Member
 
Join Date: Feb 2015
Posts: 2
Rep Power: 0
sinsni is on a distinguished road
Greetings! Hope someone will be able to give some hints on my issue. Thanks in advance.

I'm doing ray tracing, using octree:
Code:
indexedOctree<treeDataFace> faceTree     
(         
     treeDataFace(false, mesh),         
     allBb, // overall search domain         
     8, // maxLevel         
     10, // leafsize         
     3.0 // duplicity     
);
and then
Code:
faceTree.findLine(p0, p1);
I have a mesh with millions of cells. With mesh this big, performance of the octree search becomes quite poor. But I don't need to build indexedOctree from the whole mesh, like I do:
Code:
treeDataFace(false, mesh),
All my rays are in a restricted part of the mesh. Is it possible to do kind of like this:
Code:
treeDataFace(false, "small part of the mesh"),
Ultimately, I want to improve performance. Maybe there's some other way...
sinsni is offline   Reply With Quote

Old   December 8, 2015, 09:21
Default
  #16
Member
 
David GISEN
Join Date: Jul 2009
Location: Germany
Posts: 70
Rep Power: 17
David* is on a distinguished road
Quote:
Originally Posted by mirx View Post
For a ray tracing application I think you will be interested in the first intersection of a ray with a surface. In this case you may use method findLine of indexedOctree<treeDataTriSurface>.

This is a short example how to use this method:

Code:
const triSurface surf(runTime.constantPath()/"triSurface"/surfFileName);
const vectorField& normals = surf.faceNormals();
const triSurfaceSearch querySurf(surf);
const indexedOctree<treeDataTriSurface>& tree = querySurf.tree();
point searchStart(0, 0, 0);
point searchEnd(1, 0, 0);
pointIndexHit pHit = tree.findLine(searchStart, searchEnd);
if ( pHit.hit() )
{
    point hitPoint = pHit.hitPoint();
    label hitTriangle = pHit.index();
    vector normal = normals[hitTriangle];
}
...
Small note to save others the amounts of time I spent on this: To use this (very useful!) code without the wmake script (i.e., your own Makefile), it is necessary to apply the -DNoRepository compiler flag. Otherwise, you'll run into something like
Code:
undefined reference to `Foam::PrimitivePatch<Foam::labelledTri, Foam::List, Foam::Field<Foam::Vector<double> >, Foam::Vector<double> >::faceNormals() const'
because PrimitivePatch.H is a template class and does not automatically include the necessary .C files.

Cheers, David
David* is offline   Reply With Quote

Old   April 21, 2017, 11:58
Default
  #17
Member
 
Thomas Flint
Join Date: Jan 2016
Posts: 60
Rep Power: 10
tom_flint2012 is on a distinguished road
Sinsi,

Did you ever find a method of restricting the search to a smaller part of the mesh?

All the best,

Tom
treeDataFace(false, "small part of the mesh"),
tom_flint2012 is offline   Reply With Quote

Old   December 14, 2017, 14:14
Default
  #18
Member
 
Ashish Kumar
Join Date: Jun 2015
Posts: 33
Rep Power: 11
ashish.svm is on a distinguished road
is it possible to search the intersection for the user specified cells?
ashish.svm is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Superlinear speedup in OpenFOAM 13 msrinath80 OpenFOAM Running, Solving & CFD 18 March 3, 2015 06:36
OpenFOAM Foundation Releases OpenFOAMŪ Version 2.1.1 opencfd OpenFOAM Announcements from ESI-OpenCFD 0 May 31, 2012 10:07
OpenFOAM Training in Europe and USA hjasak OpenFOAM 0 August 8, 2008 06:33
question on ray tracing Ramesh Main CFD Forum 3 February 9, 2006 06:23
Looking for paper: Ray Tracing Complex Scenes lemas Main CFD Forum 0 September 9, 2003 08:38


All times are GMT -4. The time now is 10:52.