P
pereges
Ok, I have some problem with arrays which i want to use for storing
rays in my ray tracing project. please have a little patience to read.
I need to fire rays from a a rectangular plane. The rays are parallel
to each other so their direction is same but they differ in their
origin points or source location. I tried to determine the source
location by creating a grid of the rectangular plane. The rays are
going to be spaced from each other at some distance lets say incr. The
rectangular plane is going to be divided into small squares each of
length incr.
unsigned long int numberofrays;
so
incr = sqrt(l * b / numberofrays)
now lets say the plane is represented by four corners:
xmin, ymin
xmax, ymin
xmin, ymax
xmax, ymax
all are doubles
My ray data structure is :
typedef struct ray_struct
{
double ox, oy, oz; // ray origin or source represented by the
3 x, y, z coordinates
double t; //distance travelled
double dx, dy, dz; // ray direction represented by 3 x, y, z
coordinates
}ray;
Now, this is what I did to create the list of rays:
ray *ray_list;
ray_list = calloc(sizeof(ray), numberofrays);
Then, I try to store the rays:
double xcord, ycord; // represents the origin of a random ray
int i = 0;
for(ycord = ymin; ycord <= ymax; ycord += incr)
{
for(xcord = xmin; xcord<= xmax; xcord+= incr)
{
ray_list.ox = x;
ray_list.oy = y;
ray_list.oz = 1000;
ray_list.dx = 0; // all rays parallel to each other and
travelling in +z direction
ray_list.dy = 0;
ray_list.dz = 1;
ray_list.t = DBL_MAX; // rays go till infinity
i++;
}
}
Unfortunately, with this approach I am getting a segfault error.
Also, when I entered the ray number as 20,000 actually there were
20164 rays getting created which means I had crossed the array bounds.
I could store these rays in a link list or a file but I think it is
not a good idea at all. Also, what I could do is ask the user to enter
incr first and based on that apply the above loop to calculate the
number of rays and then using that number i allocate an array of rays
and then add data sequentially. This approach is seeming too naive to
me and also does not allow user to enter number of rays.
One other problem I have is that in my project, it is possible that a
ray hits an object and a reflected ray is generated and this
reflected ray hits the object again and spawns a new ray. I need it
for some energy calculations and this allows me to traverse the entire
optical path of a ray. I was wondering if it is ok to have child
pointer to solve this problem :
typedef struct ray_struct
{
........
........
struct ray_struct ray *child;
}
IF a ray does not intersect the object then it spawns no rays and
child is set to NULL
I also want to ask, how useful is it to have doubles in a numerical
simulation program(high accuracy is desired) ? Or is it ok to use
floats instead ? Using doubles has really slowed down my program.
Another thing I have noted is that its best to have variables with
scopes as small as possible. I still wonder why many people prefer to
use :
extern const double PI = 3.14
over
#define PI 3.14
shouldn't the second option be more efficient ?
rays in my ray tracing project. please have a little patience to read.
I need to fire rays from a a rectangular plane. The rays are parallel
to each other so their direction is same but they differ in their
origin points or source location. I tried to determine the source
location by creating a grid of the rectangular plane. The rays are
going to be spaced from each other at some distance lets say incr. The
rectangular plane is going to be divided into small squares each of
length incr.
unsigned long int numberofrays;
so
incr = sqrt(l * b / numberofrays)
now lets say the plane is represented by four corners:
xmin, ymin
xmax, ymin
xmin, ymax
xmax, ymax
all are doubles
My ray data structure is :
typedef struct ray_struct
{
double ox, oy, oz; // ray origin or source represented by the
3 x, y, z coordinates
double t; //distance travelled
double dx, dy, dz; // ray direction represented by 3 x, y, z
coordinates
}ray;
Now, this is what I did to create the list of rays:
ray *ray_list;
ray_list = calloc(sizeof(ray), numberofrays);
Then, I try to store the rays:
double xcord, ycord; // represents the origin of a random ray
int i = 0;
for(ycord = ymin; ycord <= ymax; ycord += incr)
{
for(xcord = xmin; xcord<= xmax; xcord+= incr)
{
ray_list.ox = x;
ray_list.oy = y;
ray_list.oz = 1000;
ray_list.dx = 0; // all rays parallel to each other and
travelling in +z direction
ray_list.dy = 0;
ray_list.dz = 1;
ray_list.t = DBL_MAX; // rays go till infinity
i++;
}
}
Unfortunately, with this approach I am getting a segfault error.
Also, when I entered the ray number as 20,000 actually there were
20164 rays getting created which means I had crossed the array bounds.
I could store these rays in a link list or a file but I think it is
not a good idea at all. Also, what I could do is ask the user to enter
incr first and based on that apply the above loop to calculate the
number of rays and then using that number i allocate an array of rays
and then add data sequentially. This approach is seeming too naive to
me and also does not allow user to enter number of rays.
One other problem I have is that in my project, it is possible that a
ray hits an object and a reflected ray is generated and this
reflected ray hits the object again and spawns a new ray. I need it
for some energy calculations and this allows me to traverse the entire
optical path of a ray. I was wondering if it is ok to have child
pointer to solve this problem :
typedef struct ray_struct
{
........
........
struct ray_struct ray *child;
}
IF a ray does not intersect the object then it spawns no rays and
child is set to NULL
I also want to ask, how useful is it to have doubles in a numerical
simulation program(high accuracy is desired) ? Or is it ok to use
floats instead ? Using doubles has really slowed down my program.
Another thing I have noted is that its best to have variables with
scopes as small as possible. I still wonder why many people prefer to
use :
extern const double PI = 3.14
over
#define PI 3.14
shouldn't the second option be more efficient ?