 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
{AGUT2} {H}-IWIK Guest
|
Posted: Thu Sep 11, 2003 3:28 pm Post subject: Passing a vector<customStructure> to functions outside main( |
|
|
Hi,
I'm trying to pass my vector to a function external to my main(), but my
compiler is complaining. FYI, the struct for the facetInfo works perfectly,
it's just the vector passing.
A quick overview... I have four sets of co-ordinates in each 'facetInfo',
and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects
that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo'
vector with the remaining 'facetInfo's into my main().
Here's a stripped down version....
****** Begin Code Snippet ******
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// Creates the basic framework for the facets to be read in
struct facetInfo {
double v1x;
double v1y;
double v1z;
double v2x;
double v2y;
double v2z;
double v3x;
double v3y;
double v3z;
double nox;
double noy;
double noz;
}; // Semicolon necessary
int main()
{
vector<facetInfo> mySurface;
facetInfo holdMe; // Holder facet to allow push_back() function.
ifstream inf("input.stl");
char buffer[128]; // File input buffer - 128 letter maximum
while(!inf.getline(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push_back(holdMe);'s
}
}
vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{
if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhile
return newHull;
}
****** End Code Snippet ******
The compiler says 'v1x' [and all the others] is not a member of
'vector' in function
cutZeros(vector<facetInfo,allocator)
Now, I read something about changing the cutZeros function to:
****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInfo>& facinf = mySurface;
// out of main()
vector<facetInfo> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
...
return newHull;
}
****** End Code Snippet ******
But the compiler says that 'size' is not a member of 'facetInfo' in
function 'cutZeros(const facetInfo &)'
Am I able to do this, or am I living in false hope?
Thank you again for your help, c.l.c++ - When I have a modicum of skill I
will pay your kindness back :)
Alex Livingstone
--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Sep 11, 2003 3:37 pm Post subject: Re: Passing a vector<customStructure> to functions outside m |
|
|
"{AGUT2} {H}-IWIK" <alexan.tilivingstonesp (AT) ambtinternet (DOT) com> wrote in message
| Quote: | holder.v1x = mainHull.v1x;
|
mainHull isn't a facetinfo, it's a vector<facitinfo>
If you want to extract atNum's entry you need to do
holder.v1x = mainHull[atNum].v1x;
Actually, there's no reason to individually copy each member.
holder = mainHull[atNum];
will work just fine.
|
|
| Back to top |
|
 |
{AGUT2} {H}-IWIK Guest
|
Posted: Thu Sep 11, 2003 3:49 pm Post subject: Re: Passing a vector<customStructure> to functions outside m |
|
|
| Quote: | | mainHull isn't a facetinfo, it's a vector
|
Thank you. Consider me officially slapped 'upside the head'.
I apologise for the silly question.
Alex.
--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Thu Sep 11, 2003 3:53 pm Post subject: Re: Passing a vector<customStructure> to functions outside m |
|
|
{AGUT2} {H}-IWIK wrote in news:oprvcc9djtp508op (AT) mercury (DOT) nildram.net:
| Quote: | Hi,
I'm trying to pass my vector to a function external to my main(), but
my compiler is complaining. FYI, the struct for the facetInfo works
perfectly, it's just the vector passing.
[snip]
vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{
|
Note in this case:
vector<facetInfo> cutZeros(vector<facetInfo> const & mainHull)
{
Would be more eficient, as you don't modify mainHull.
| Quote: | facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{
|
simplest option is to change mainHull.v1y etc below to
mainHull[ atNum ].v1y.
vector< facetInfo > is an indexable container of facetInfo's its
the [ atNum ] that selects the item.
| Quote: | if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhil
return newHull;
}
****** End Code Snippet ******
|
[snip]
HTH
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Thu Sep 11, 2003 6:09 pm Post subject: Re: Passing a vector<customStructure> to functions outside m |
|
|
"{AGUT2} {H}-IWIK" <alexan.tilivingstonesp (AT) ambtinternet (DOT) com> wrote in
message news:oprvcd8enqp508op (AT) mercury (DOT) nildram.net...
| Quote: | mainHull isn't a facetinfo, it's a vector<facitinfo
Thank you. Consider me officially slapped 'upside the head'.
I apologise for the silly question.
Alex.
|
Just some other comments. You should consider implementing a 3D vector
class/struct which will make your code more legible:
struct facetInfo {
CVector m_Vertex1;
CVector m_Vertex2;
CVector m_Vertex3;
CVector m_Normal;
}; // Semicolon necessary
A simple implementation of a 3D vector coud look like this:
class CVector
{
public :
CVector() : X(0), Y(0), Z(0) {}
CVector ( double x, double y, double z ) : X(x), Y(y), Z(z) { }
CVector (const CVector& v) { X = v.X; Y = v.Y; Z = v.Z; }
virtual ~CVector () {}
CVector operator + (const CVector& v ) const { return CVector( X + v.X, Y
+ v.Y, Z + v.Z ); }
CVector operator - (const CVector& v ) const { return CVector( X - v.X,
Y - v.Y, Z - v.Z ); }
CVector operator -() const { return CVector( -X, -Y, -Z ); }
CVector operator / (const double Denominator) const { return CVector( X /
Denominator, Y / Denominator, Z / Denominator ); }
CVector operator / (const CVector& Denominator) const { return CVector( X
/ Denominator.X, Y / Denominator.Y, Z / Denominator.Z ); }
CVector operator * (const double Multiplicator) const { return CVector( X
* Multiplicator, Y * Multiplicator, Z * Multiplicator ); }
CVector operator * (const CVector& v) const { return CVector( X * v.X, Y *
v.Y, Z * v.Z ); }
bool operator == (const CVector& v) const { return (fabs(X - v.X) <
EPSILON) && (fabs(Y - v.Y) < EPSILON) && (fabs(Z - v.Z) < EPSILON); }
bool operator != (const CVector& v) const { return ! (*this == v); }
const CVector& operator += (const CVector& v) { X += v.X; Y += v.Y; Z +=
v.Z; return *this; }
const CVector& operator -= (const CVector& v) { X -= v.X; Y -= v.Y; Z -=
v.Z; return *this; }
const CVector& operator *= (const CVector& v) { X *= v.X; Y *= v.Y; Z *=
v.Z; return *this; }
const CVector& operator *= (const double m) { X *= m; Y *= m; Z *= m;
return *this; }
const CVector& operator /= (const CVector& v) { X /= v.X; Y /= v.Y; Z /=
v.Z; return *this; }
const CVector& operator /= (const double d) { X /= d; Y /= d; Z /= d;
return *this; }
CVector CrossProd( const CVector& v ) const { return CVector( Y * v.Z - Z
* v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X ); }
double DotProd( const CVector& v ) const { return X * v.X + Y * v.Y + Z *
v.Z; }
void Set( double x, double y, double z ) { X = x; Y = y; Z = z; }
void Get( double& x, double& y, double& z ) const { x = X; y = Y; z = Z; }
double Norm() const { return (double)( X*X ) + (double)( Y*Y ) +
(double)(Z*Z);}
double Length() const { return sqrt( Norm() ); }
double Distance( const CVector& v ) const { return (v - *this).Length(); }
CVector Min( const CVector& v ) const { return CVector( X < v.X ? X : v.X,
Y < v.Y ? Y : v.Y, Z < v.Z ? Z : v.Z ); };
CVector Max( const CVector& v ) const { return CVector( X > v.X ? X : v.X,
Y > v.Y ? Y : v.Y, Z > v.Z ? Z : v.Z ); };
double X;
double Y;
double Z;
};
HTH
Chris
|
|
| Back to top |
|
 |
Loran Hayden Guest
|
Posted: Wed Feb 04, 2004 4:54 am Post subject: Re: Passing a vector<customStructure> to functions outside m |
|
|
mainHull is a vector, not a facetInfo struct. that might clear up anything
else. (and why are you passing by value instead of reference?)
"{AGUT2} {H}-IWIK" <alexan.tilivingstonesp (AT) ambtinternet (DOT) com> wrote in
message news:oprvcc9djtp508op (AT) mercury (DOT) nildram.net...
| Quote: | Hi,
I'm trying to pass my vector to a function external to my main(), but my
compiler is complaining. FYI, the struct for the facetInfo works
perfectly,
it's just the vector passing.
A quick overview... I have four sets of co-ordinates in each 'facetInfo',
and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects
that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo'
vector with the remaining 'facetInfo's into my main().
Here's a stripped down version....
****** Begin Code Snippet ******
#include <cstdlib
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Creates the basic framework for the facets to be read in
struct facetInfo {
double v1x;
double v1y;
double v1z;
double v2x;
double v2y;
double v2z;
double v3x;
double v3y;
double v3z;
double nox;
double noy;
double noz;
}; // Semicolon necessary
int main()
{
vector
facetInfo holdMe; // Holder facet to allow push_back() function.
ifstream inf("input.stl");
char buffer[128]; // File input buffer - 128 letter maximum
while(!inf.getline(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push_back(holdMe);'s
}
}
vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{
if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhile
return newHull;
}
****** End Code Snippet ******
The compiler says 'v1x' [and all the others] is not a member of
'vector' in function
cutZeros(vector<facetInfo,allocator)
Now, I read something about changing the cutZeros function to:
****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInfo>& facinf = mySurface;
// out of main()
vector<facetInfo> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
...
return newHull;
}
****** End Code Snippet ******
But the compiler says that 'size' is not a member of 'facetInfo' in
function 'cutZeros(const facetInfo &)'
Am I able to do this, or am I living in false hope?
Thank you again for your help, c.l.c++ - When I have a modicum of skill I
will pay your kindness back :)
Alex Livingstone
--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|