daviddoria 334 Posting Virtuoso Featured Poster

I would use OpenGL. It available for any platform and any compiler you choose.

daviddoria 334 Posting Virtuoso Featured Poster

Normally I call this function like this

void MouseButton(int button, int state, int x, int y);
...
glutMouseFunc(MouseButton);

But now I would like to pass a member function instead of a "real" (non-member? whats the word for this?) function.

class Plot
{
  void Plot::MouseButton(int button, int state, int x, int y);
  ...
  Plot() { glutMouseFunc(MouseButton); }
}

But I get this error:

/home/dave/Plot/src/Plot.h:81: error: argument of type 'void (Plot::)(int, int, int, int)' does not match 'void (*)(int, int, int, int)'

What do I have to do differently to get this to work?

Thanks!
Dave

daviddoria 334 Posting Virtuoso Featured Poster

fantastic ! exactly what I wanted.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

ah great! so

g++ -DGRAPHICS

is the same as having all the files being compiled having a #define GRAPHICS 1 ?

so then I could even use
if(GRAPHICS)

instead of
#ifdef GRAPHICS

?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I would like to have a global way to do something like this

#ifdef GRAPHICS
//do some stuff that relies on graphics libraries that may or may not be installed
#endif

The problem with defines is that they have to be defined in every file, or at least be defined in a header that is included in every file that I wish to do this.

What I'm looking for is more of maybe a compiler option or something? Where I can say

g++ MyProgram +GRAPHICS
or
g++ MyProgram -GRAPHICS

And it will just ignore parts of the code completely if GRAPHICS is not specified.

Sorry if this is unclear, but I'll explain as we go! haha

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

monkey_king: I agree, it should definitely have numerical gradients. Also, its annoying that you have to use gsl_vector, and then gsl_vector_allocate, and then gsl_vector_get and _set. Its also annoying how you have to pass parameters as a void pointer.

arkm: what an accurate and complete answer! it seems your answer is more annoying / less helpful than my question. you posted a link to a page with 1000 links, one of which may be relevant.

daviddoria 334 Posting Virtuoso Featured Poster

I have been using GSL, but I find it very awkward to work with. Does anyone have a recommendation for another one to try? I can't seem to find any on google.

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

It seems that writing ascii files changed significantly from c to c++. It moved from file pointers and things like that to being very easy, like

ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.";
  myfile.close();

However, from googling it seems that binary file reading/writing has not become any easier - you still have to keep track of the sizeof() what you are writing and input using buffers and all that. Am I correct? or is there indeed an easy way that I just didn't find?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Pretty harsh, don't you say? I clearly did not know the difference in a lot of these things, hence my post on a forum....

And also, the question was indeed about the time taken to store and retrieve these values, not to perform any operations on them. If the answer is "it doesn't matter how you store them" then that is fine with me.

daviddoria 334 Posting Virtuoso Featured Poster

sorry, double post!

daviddoria 334 Posting Virtuoso Featured Poster

The reason I thought that arrays were so much better was from this experiment. I just wrote to a big vector and then read back from it and timed it - similar to this:

void FunctionWithVectorSubscript()
{
  vector<int> test;
  for(int i = 0; i < ArraySize; i ++)
    {
      test.push_back(i);
    }

  int temp;

  for(int i = 0; i < ArraySize; i ++)
    {
      temp = test[i];
    }


}

Here are the results. As I figured, much of the time was due to using calls like push_back() and at().

Array time: 17 ms
BestVector time(write using [], read using [] ): 39 ms
WorstVector time (write using push_back(), read using at() ): 89 ms (an addittional 50!)
VectorPushBackSubscript time(write using push_back(), read using [] ): 62 ms (an additional 20)
VectorSubscriptAt time(write using [], read using at() ): 50 ms (an additional 10)

But it still seems to take twice as long even with the best way of using a vector vs an array.

Thoughts? (I can attach the entire "test" program if you want, it was just alot of lines so I just gave the one example function)

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I am not really doing operations on the vector, I am just storing values and retrieving them later. The problem is there are just lots and lots of them. I'll look into valarray's, but for now, if I use a standard array

int *myArray = new int [ somesize ];

don't I have to then manually delete the variable when I'm done with it?

Also, how can you get the length of the array after the fact (ie. the equivalent of MyVector.size() )?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

you're right, I switched the order and in both cases the first one took longer!!

How would you make an array of size determined at run time if you're compiler doesn't allow this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have been using vector in c++ lately, but I needed to speed up my code a bit so someone recommended I use regular arrays. My question is 2 part -
1) I thought you couldn't do this!!?? For some reason it is working with no problem?

int a=2;
int test[a];

2) I used a timer (from the VUL library of VXL) to time the following two functions. The only difference is one of the arrays is allocated using a global int, and the other is allocated using a #define. It actually turns out the the global int is twice as fast!!?? I thought they should be about the same (if the int even worked at all! (see question 1))

FunctionWithArray time: 17 ms
FunctionWithDynamicArray time: 8 ms
#include <iostream>

using namespace std;

int DynamicSize = 2000000;
#define ArraySize 2000000

void FunctionWithDynamicArray();
void FunctionWithArray();

int main()
{
FunctionWithDynamicArray();
FunctionWithArray();
return 0;
}

void FunctionWithDynamicArray()
{
 int test[DynamicSize];
  for(int i = 0; i < DynamicSize; i ++)
    {
      test[i] = i;
    }
 }

void FunctionWithArray()
{
  int test[ArraySize];
  for(int i = 0; i < ArraySize; i ++)
    {
      test[i] = i;
    }
}

Any thoughts on either of these?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

awesome - sounds about perfect. Can I put the GlobalOptions class in a separate file (GlobalOptions.h) and then include it normally (ie. #include "GlobalOptions.h) and use it the same way?

Also, does it have to be in a namespace?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I want to be able to do this:

Pass a parameter to my main program like "parallel = yes"

and then many functions down in the hierarchy (ie main calls "Function1" which calls "Function2" which calls "Function3", etc) I need to see the value of "parallel". I'd hate to have to pass it to every function along the way even though they will never look at it. Is there a better way to do this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

ahhh that was the key ! Needed to use B instead of B()

Thanks!

daviddoria 334 Posting Virtuoso Featured Poster

I have a very simple class

class Point
{

public:
Point();
Point(double x, double y);
  double x_;
double y_;
}

the problem is, I can't assign my class to another instance, ie:

Point A(2,4);
Point B();
B = A;

It says the lvalue must be modifiable. Why is it not?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

actually is there a way to do this without dealing with all the QT nonsense?

daviddoria 334 Posting Virtuoso Featured Poster

Man, right again - I wish it would give more informative errors sometimes...

daviddoria 334 Posting Virtuoso Featured Poster

Oh sorry, typo in the post. They should all be Min.

So why can't I put the & there?

daviddoria 334 Posting Virtuoso Featured Poster

well i fixed it by taking the & out of the Max() declaration....

but then isn't it passing the entire array (which is very big in this case) instead of just its address?

daviddoria 334 Posting Virtuoso Featured Poster

I have a function

double Max(vector<geom_Point3> &Points, int n);

Then in another class, I have a private member:
vector<geom_Point3> Vertices_;

and in a class function, I call:
min_x = Min(Vertices_, 0);

however, I get

error: qualifiers dropped in binding reference of type "std::vector<geom_Point3, std::allocator<geom_Point3>> &" to initializer of type "const std::vector<geom_Point3, std::allocator<geom_Point3>>"
    min_x = Min(Vertices_, 0);

What does that mean??

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to overload *

class Vector3
{
public:
  geom_Vector3 operator * (double d);//overload * from the left
};

geom_Vector3 operator * (double d, geom_Vector3 &V); //overload * from the right

The problem is this works

geom_Vector3 V(1,2,3);
cout << -1 * V << endl;

but this doesn't

cout << -1 * geom_Vector3(1,2,3) << endl;

Do I have to also have a * for const Vector3's somehow?

Thanks!
Dave

daviddoria 334 Posting Virtuoso Featured Poster

sure sure, but then how does it know which functions to use? Like then I'd have to have an Intersect2 and an Intersect3 function , right? So we've just moved the problem?

daviddoria 334 Posting Virtuoso Featured Poster

Point2 contains
double x, y;

Point3 contains
double x,y,z;

but they have different functions. Well in the case of Point they dont really have any functions at all, but with like my Line2 class, it has an IntersectLine() function that uses the y=mx+b type equations, which clearly do not apply in 3d. so my Line3 should also have an IntersectLine() function, but it will be very different.

daviddoria 334 Posting Virtuoso Featured Poster

Right now this is my setup:

I have these classes
Point2
Point3
Vector2
Vector3
Ray2
Ray3

Both Point classes and both Vector are independent. Ray2 contains a Point2 and a Vector2 object, and Ray3 contains a Point3 and a Vector3 object.

I have two questions.

1) Should I somehow just have a Point class that has a way to make a Point2 and a Point3 (and same for the Vector classes)

2) As I mentioned, the Ray classes just contain a Point and a Vector of the same dimension as the ray. Should this be a separate class? or again somehow derived from one of the other ones?

I have a very basic understanding of this idea and of virtual classes- if you have a class "animal" it doesn't make sense to give it a name, you first have to make a subclass "dog" before you can name it, but I've never implemented either of these ideas so I'm not sure when to apply them.

Any light you can shed will be much appreciated!

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

another vote for stl vectors!!

daviddoria 334 Posting Virtuoso Featured Poster

I have neither an assignment operator or virtual destructor, so then why do i need a copy constructor?

Can you give a simple example of each of those?

daviddoria 334 Posting Virtuoso Featured Poster

I have a function Scan.MakeLinearGrid() which returns a vector<vector<Vector3> >

I have another function that is made to accept this type of thing:
Scan.setAngleList(vector<vector<Vector3> >);

But if I call it like this:

Scan.setAngleList(Scan.MakeLinearGrid());

it says

error: initial value of reference to non-const must be an lvalue
    Scan.setAngleList(Scan.MakeLinearGrid());

but if i do

vector<vector<Vector3> > temp = Scan.MakeLinearGrid();
Scan.setAngleList(temp);

it works fine.

Can someone explain the difference?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

ok, i'll start making that a habbit

here's another question though:

Vector3 Plane::Normal()
{
        double norm = sqrt(pow(A,2) + pow(B,2) + pow(C,2));
	geom_Vector3 V(A/norm, B/norm, C/norm);
	return V;
}

then in the << function, I can't do

... << P.Normal();

because Normal() isn't a const function. However, I should be able to do

geom_Vector3 N = P.Normal();
  output << "Plane Normal: " << N << endl;

but it's telling me

error: the object has cv-qualifiers that are not compatible with the member function
            object type is: const Plane
    Vector3 N = P.Normal();

Why can't I do that??

daviddoria 334 Posting Virtuoso Featured Poster

I posted a while back about how to overload <<

It seems all the examples online have the second argument as const, ie

ostream & operator << (ostream &output, const Point &p);

but if I have a member function that simply returns a double

double Point::getX()
{
return x_;
}

In the << function it complains about the const

error: the object has cv-qualifiers that are not compatible with the member function
            object type is: const Point

So I've fixed it two ways:
1) make the getX() function return a const double
2) change the << function to not accept a const, ie

ostream & operator << (ostream &output, const Point &p);

So should accessor functions always return const? Or is it ok to remove const from the << definition?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

bahhh I didn't have ScanScene.o in my FILELIST in my makefile.

Maybe this is a good second question starter - how do you guys create your makefiles? It seems really easy to forget something like I just did when you have a decent size project.

I have been writing them by hand -

INCLUDES = -I../GeometryLibrary -I../ObjReader

#LIBS = -L../GeometryLibrary -lgeom -L/usr/local/lib -openmp
LIBS = -L../GeometryLibrary -lgeom -lstdc++

FILELIST = SingleScan.o ScanScene.o LiDARScanner.o LiDARScan.o WriteFiles.o ScanPoint.o

EXTERNALFILES = ../ObjReader/ObjReader.o

COMPILER = icc

FLAGS = -fPIC

SingleScan: $(FILELIST)
	$(COMPILER) $(INCLUDES) $(FILELIST) $(EXTERNALFILES) -o SingleScan $(LIBS) $(FLAGS)

#ConcentricScan.o: ConcentricScan.cpp
#	$(COMPILER) -c ConcentricScan.cpp -o ConcentricScan.o $(LIBS) $(FLAGS)

SingleScan.o: SingleScan.cpp
	$(COMPILER) $(INCLUDES) -c SingleScan.cpp -o SingleScan.o $(LIBS) $(FLAGS)

LiDARScan.o: LiDARScan.cpp LiDARScan.h
	$(COMPILER) $(INCLUDES) -c LiDARScan.cpp -o LiDARScan.o $(LIBS) $(FLAGS)

.....

Does that look ok? Or are there better ways to do this?

daviddoria 334 Posting Virtuoso Featured Poster

(see next post for the question about makefiles!)

I have this line in ScanScene.h

bool ScanScene(LiDARScan &Scan, LiDARScanner &Scanner, vector<geom_Triangle> &Scene);

and in ScanScene.cpp

bool ScanScene(LiDARScan &Scan, LiDARScanner &Scanner, vector<geom_Triangle> &Scene)
{
...
}

then in another file, I

#include "ScanScene.h";
int main()
{
LiDARScan Scan;
LiDARScanner Scanner;
vector<geom_Triangle> Triangles;
 bool success = ScanScene(Scan, Scanner, Triangles); 
}

But when I compile it says

SingleScan.o(.text+0x339): In function `main':
: undefined reference to `ScanScene(LiDARScan&, LiDARScanner&, std::vector<geom_Triangle, std::allocator<geom_Triangle> >&)'

Can anyone see a problem with this?

daviddoria 334 Posting Virtuoso Featured Poster

hmm I looked at ncurses (very cool - i've always wanted to be able to use the arrow keys and function keys). I saw getch(), but nothing that doesn't stop the execution( ie. no kbhit() function).

Am I just missing it?

daviddoria 334 Posting Virtuoso Featured Poster

Salem - I'm using linux and g++ or icc

Ancient Dragon - I'll look into those.

~Dave

daviddoria 334 Posting Virtuoso Featured Poster

Is there a way to check for a keypress every time through a loop without forcing a key to be pressed. ie. if you use scanf, the loop will not continue unless a key is pressed. I'd like to say "if a key was pressed, handle it. If not, keep going"

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

ok folks, i couldn't find anything that wasn't crazy overly complicated, so I wrote it myself:
http://doriad.myrpi.org/ObjReader.cpp

The triangle intersection is a bit harder to post because it uses a big geometry library that I have written, but I'll post the main intersection function once I test it.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

make a file using any text editor (emacs, vi, gedit) called test.cpp

#include <iostream>
using namespace std;

double Multiply(double a, double b)
{
return a*b;
}

int main()
{
double c = Multiply(10,2);
cout << "10 * 2 = " << c << endl;
return 0;
}

then to compile, from a terminal type

g++ test.cpp -o test

then to run the program

./test

hope this helps

dave

daviddoria 334 Posting Virtuoso Featured Poster

I dont see where you set the delimiter? Is it magically splitting the date string at '/' ??

daviddoria 334 Posting Virtuoso Featured Poster

I have a file that contains lines like:

2/3/4 5/6/7 8/9/10

and I want to extract the 2, the 5, and the 8

So I am parsing the line by the ' ' delmiter to get
2/3/4
5/6/7
8/9/10

then I need to parse each of those to get the stuff up to the first '/'

indata.open(filename);
string line;
getline(indata, line);

//line is now "hello world yay"

stringstream Parsed(line);

//I can read to ' ' delimiter with >>

string a, b, c;
Parsed >> a; // a == "hello"
Parsed >> b;  // b == "world"
Parsed >> c; // c == "yay"

//Now I want to parse a by the '/' character
string d;
getline(a, d, '/');

I get this error

error: no instance of overloaded function "getline" matches the argument list
argument types are: (std::string, std::string, char)

Why does this not work?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Right, but I was wondering if anyone has this implemented? I would rather spend time on my actual work instead of implementing this that has surely been implemented 1000 times before!

daviddoria 334 Posting Virtuoso Featured Poster

Anyone know where I can get an implementation of this? Some code to read in a set of vertices/triangles and then do some simple intersection tests? If you can point me in the right direction that'd be great!

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have about a 1gb data file that needs to be read at the beginning of my program. This takes about 2 minutes to do. I feel like the answer is "no way", but here is my question.

In matlab, there is the concept of a "workspace". I can ready my giant file into the workspace, then run code that accesses this data, then modify the code, and run it again on the same data without reading it in again. Is there any idea similar to this with c++?

daviddoria 334 Posting Virtuoso Featured Poster

I have a function:
void CreateCostGrid(vector<ScanPoint> &Data, int whichcol, vector<LiDARScan> &LiDARGrid);

That I call like this:
CreateCostGrid(TestScan.getColumn(GoodCols.at(testcol)), GoodCols.at(testcol), ModelScans);

But I get this:
error: initial value of reference to non-const must be an lvalue
CreateCostGrid(TestScan.getColumn(GoodCols.at(testcol)), GoodCols.at(testcol), ModelScans);
(There is an arrow underneath the T in TestScan, i guess indicating that is there the problem is?)

This has been working for weeks and all of a sudden stopped working - so there is nothing wrong with any of the things you can't see here (ie. the ScanPoint class or the LiDARScan class).

Anyone know what this error means?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

What if I am not constructing V1 (it already exists)?

V1.clear();

and then...?

daviddoria 334 Posting Virtuoso Featured Poster

I'd imagine there is a much better way to do this...

vector<double> Model;
//code to fill Model
vector<double> V1;

for(int i = StartModel; i <= EndModel; i++)
                V1.push_back( Model.at(i) );
daviddoria 334 Posting Virtuoso Featured Poster

ok, so assuming I pass the function a const char*, I'd need to convert it to a char* before I append anything. Do I do this?

char* newthign = const_cast<char*>(oldthing);

where newthing is now a char* and oldthing is a const char*?

daviddoria 334 Posting Virtuoso Featured Poster

I want to do something like this

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
  MyFunc("test");
  return 0;
}

void MyFunc(const char* Filename)
{
  cout << strcat(Filename, ".tst") << endl;
}

but it tells me that my const char* cannot be used where it is expecting char*

Is the solution simply to accept it as a char* instead of a const char*? I thought it was always better to have things const if you weren't going to modify them?

Thanks

Dave

daviddoria 334 Posting Virtuoso Featured Poster

ah - i see, i wasn't throwing anything, so clearly it was not being caught! Thanks for the link.