Re: how to organize my main file ?



pereges said:

By main file, I mean the one which contains the main routine. Can some
one please provide suggestions as to how I can improve the
organization of main file ? I have just though about a rough skeleton.

In my ray tracing project, I have to carry out following task (in
sequence)

1. Read the mesh from an ascii file and store it in the mesh data
structure.
2. Create a ray list and store it in a ray list.
3. Create the binary space partitioning tree for fast mesh traversal.
4. Trace all the rays and calculate the scattered and incident
electric fields.

I'm thinking of writing a function for every task.

It seems to me that tasks 1-3 might reasonably be called initialisation.
Task 4 appears to be the bit that does the useful processing.

A function for every task is always a good idea, but why not abstract your
first three tasks into a function called initialise() or something like
that. It might look something like this:

#include "whatever.h"

int initialise(const char *infile)
{
int rc = read_mesh(infile); /* change read_mesh() to take const char *
*/
if(0 == rc)
{
rc = init_plane_wave();
}
if(0 == rc)
{
rc = create_bsp_tree();
}
return rc;
}

and then main would look something like this:

#include <stdio.h>
#include <stdlib.h>
#include "whatever.h"

int main(int argc, char **argv)
{
int result = EXIT_FAILURE;

if(argc != 2)
{
printf("%s arguments\n", argc < 2 ? "Insufficient" : "Too many");
}
else
{
if(0 == initialise(argv[1]))
{
if(0 == calc_e_fields())
{
result = EXIT_SUCCESS;
}
}
}
return result;
}

You might want to put main() and initialise() in one source file (your
"main file" as you call it), the initialisation routines (read_mesh and
the other two) in, say, initialise.c, and the calcs in calcs.c - this will
help to keep each source file down to a manageable size and make things
easier for you to find.

Note that a -1 return value from main isn't guaranteed to be meaningful,
whereas EXIT_FAILURE is.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Exploring Static Init
    ... After Thomas surprised me with the enum decompilation, ... static final int inlined = 2; ... Initialisation proceeds mindlessly top to bottom. ...
    (comp.lang.java.programmer)
  • Re: removing a loop cause it to go at half the speed?
    ... Without the initialisation, 1/4 of the array-accesses will result in a cache miss, and the processor will have to wait for the bus to grab the data from RAM. ... IIRC linux has a special flag in the process-structure, which is set whenever the process uses the FPU. ... int count = COUNT; ...
    (comp.unix.programmer)
  • [LONG] help with old source code from book
    ... int cols, int magic_number); ... scale = 1.0; ... reads mesh data into a mesh structure * ...
    (comp.lang.c)
  • Re: Question about pointers
    ... fact, recommend the opposite. ... int Answer = 42; ... that's an initialisation ). ... Don't be afraid of initialising objects when you define them. ...
    (comp.lang.c)
  • Re: SetIndexBufferData
    ... int slices = _slices.animVal; ... Microsoft.DirectX.Direct3D.Mesh mesh = new ... Have you tried with an array ... > example in the help file, but with just partial success, right now, ie, ...
    (microsoft.public.win32.programmer.directx.managed)