What is wrong with the following C++ class? - c++

So, basically I created a Float class to track down the range of floating point values used in the program. I just replace float in my program with Float. However, the output of my program changes when I use this class, but I can't figure out where is the problem.
#define real float
class Float
{
private:
real data;
static real minVal;
static real maxVal;
static void updateMinMax( real x )
{
if ( x < minVal )
minVal = x;
if ( x > maxVal )
maxVal = x;
}
public:
static real getMin()
{
return minVal;
}
static real getMax()
{
return maxVal;
}
Float()
{
data = 0;
}
Float(real x)
{
data = x;
updateMinMax(data);
}
void setFloat( real x )
{
data = x;
updateMinMax(data);
}
void setMaxOf( real x, real y )
{
data = (x > y)? x : y;
updateMinMax(data);
}
void setInt( int x )
{
data = x;
updateMinMax(data);
}
real getFloat() const
{
return data;
}
operator int() const { return (int)data; }
void operator=(Float x)
{
data = x.data;
updateMinMax( data );
}
Float operator+(const Float& x) const
{
updateMinMax( data + x.data );
return Float(data + x.data);
}
void operator+=(Float x)
{
data += x.data;
updateMinMax( data );
}
Float operator-(const Float& x) const
{
updateMinMax( data - x.data );
return Float(data - x.data);
}
Float operator-() const
{
updateMinMax( -data );
return Float(-data);
}
void operator-=(Float x)
{
data -= x.data;
updateMinMax( data );
}
Float operator*(const Float& x) const
{
updateMinMax( data * x.data );
return Float(data * x.data);
}
void operator*=(Float x)
{
data *= x.data;
updateMinMax( data );
}
Float operator/(const Float& x) const
{
updateMinMax( data / x.data );
return Float(data / x.data);
}
void operator/=(Float x)
{
data /= x.data;
updateMinMax( data );
}
bool operator<(const Float& x) const
{
return data < x.data;
}
bool operator<=(const Float& x) const
{
return data <= x.data;
}
bool operator>(const Float& x) const
{
return data > x.data;
}
bool operator>=(const Float& x) const
{
return data >= x.data;
}
bool operator==(const Float& x) const
{
return data == x.data;
}
friend ostream& operator<<(ostream& o, Float& x)
{
o << x.data;
return o;
}
friend istream& operator>>(istream& i, Float& x)
{
i >> x.data;
return i;
}
};

The following test program demonstrates at least one difference between using a float and your Float class:
void test(float x)
{
cout<<"Called with float argument"<<endl;
}
void test(int x)
{
cout<<"Called with int argument"<<endl;
}
int main() {
Float arg1;
float arg2;
test(arg1);
test(arg2);
return 0;
}
Output:
Called with int argument
Called with float argument

Related

Count how many times class member was printed

I need to count how many times class members were printed using function Print which is inspector. Constructor should set private elements of class.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d();
Vector3d(double x, double y, double z);
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d() {
count = 0;
}
Vector3d::Vector3d(double x, double y, double z) {
count = 0;
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
I used mutable int to enable changing element of const function. For v1.GetCount() I get output 3, which is correct. However, for v2.GetCount() I get output 5, which is wrong (correct is 2).
Could you help me to fix this? Where am I making mistake?
You need to overload copy constructor and copy assign operator for Vector3d class.
Now you are copying state of count field into v2 object, therefore it starts from 3 not from 0.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d& operator=(const Vector3d&);
Vector3d(Vector3d&&) = delete;
Vector3d& operator=(Vector3d&&) = delete;
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d& Vector3d::operator=(const Vector3d& that)
{
x = that.x;
y = that.y;
z = that.z;
return *this;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
UPDATE:
Someone mentioned that explicitly deleted move ctor and operator is not ok, I understand that, but for me it is not clear should we move counter to other instance or not. Therefore here possible implementation:
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d(Vector3d&&);
Vector3d& operator=(Vector3d);
void Print() const;
int GetCount() const;
private:
void swap(Vector3d&);
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d::Vector3d(Vector3d&& that)
: Vector3d(that.x, that.y, that.z)
{
count = that.count;
}
Vector3d& Vector3d::operator=(Vector3d that)
{
swap(that);
return *this;
}
void Vector3d::swap(Vector3d& that)
{
std::swap(x, that.x);
std::swap(y, that.y);
std::swap(z, that.z);
std::swap(count, that.count);
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2 = std::move(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
But these more for commenters than for question author.

Convex Hull Not Returning Right Path ( Graham Scan in C++)

I have done the algorithm the expected output is :
p00 - p01,
p01 - p03 ,
p03 - p10,
p10 - p12 ,
p12 - p00
But I get this instead:
Convex hull:
p00 - p01
p01 - p03
p03 - p05
p05 - p10
p10 - p00
Points:
p00: (-5,-6)
p01: (6,-4)
p02: (5.5,-3)
p03: (8,0)
p04: (5,0)
p05: (4,2)
p06: (1,3)
p07: (0,2)
p08: (-1,1)
p09: (-1.5,2)
p10: (-1.5,6)
p11: (-5.5,1.5)
p12: (-8,-1)
I have been trying so long to get it right but some how I can't. Can anyone help? I am using C++
Below is my code:
I have 3 classes Vector2D, Point2D and Point2DSet my Graham Scan Implementation is in the buildConvexHull function in the Point2DSet.
Vector2D.cpp
#include "Vector2D.h"
Vector2D::Vector2D(double aX, double aY): fX(aX), fY(aY){ }
void Vector2D::setX(double aX){ fX = aX;}
double Vector2D::getX() const { return fX; }
void Vector2D::setY(double aY) { fY = aY;}
double Vector2D::getY() const { return fY; }
Vector2D Vector2D::operator+(const Vector2D& aRHS) const
{
return (fX + aRHS.fX, fY + aRHS.fY);
}
Vector2D Vector2D::operator-(const Vector2D& aRHS) const
{
return (fX - aRHS.fX, fY - aRHS.fY);
}
double Vector2D::magnitude() const
{
return sqrt((fX * fX) + (fY * fY));
}
double Vector2D::direction() const
{
return atan(fY/fX);
}
double Vector2D::dot(const Vector2D& aRHS) const
{
return (this->getX() * aRHS.getX()) + (this->getY() * aRHS.getY());
}
double Vector2D::cross(const Vector2D& aRHS) const
{
return (this->getX() * aRHS.getY()) - (aRHS.getX() * this->getY());
}
double Vector2D::angleBetween(const Vector2D& aRHS) const
{
double dntmr = magnitude() * aRHS.magnitude();
if (dntmr > 0.0)
{
return acos(this->dot(aRHS) / (this->magnitude() * aRHS.magnitude()));
}
return acos(1.0/1.0);
}
std::ostream& operator<<(std::ostream& aOutStream, const Vector2D& aObject)
{
aOutStream << " ( " << aObject.fX << ", " << aObject.fY << " )\n";
return aOutStream;
}
std::istream& operator>>(std::istream& aInStream, Vector2D& aObject)
{
aInStream >> aObject.fX;
aInStream >> aObject.fY;
return aInStream;
}
Point2D
#include "Point2D.h"
static const Point2D gCoordinateOrigin;
// Private function gets direction in reference to aOther
double Point2D::directionTo(const Point2D& aOther) const
{
return (aOther.fPosition - fPosition).direction();
}
// Private Function to get magnitude in reference to aOther
double Point2D::magnitudeTo(const Point2D& aOther) const
{
return (aOther.fPosition - fPosition).magnitude();
}
Point2D::Point2D() : fId(" "), fPosition(0,0), fOrigin(&gCoordinateOrigin) { }
Point2D::Point2D(const std::string& aId, double aX, double aY) : fId(aId), fPosition(aX,aY), fOrigin(&gCoordinateOrigin) { }
Point2D::Point2D(std::istream &aIStream) : fOrigin(&gCoordinateOrigin)
{
aIStream >> fId >> fPosition;
}
const std::string& Point2D::getId() const { return fId; }
void Point2D::setX(const double& aX) { fPosition.setX(aX); }
void Point2D::setY(const double& aY) { fPosition.setY(aY); }
const double Point2D::getX() const { return fPosition.getX(); }
const double Point2D::getY() const { return fPosition.getY(); }
void Point2D::setOrigin(const Point2D& aPoint) { fOrigin = &aPoint;}
Vector2D Point2D::operator-(const Point2D& aRHS) const
{
return (fPosition - aRHS.fPosition);
}
// Return Direction with reference to origin
double Point2D::direction() const
{
return fOrigin->directionTo(*this);
}
// Return Direction with reference to origin
double Point2D::magnitude() const
{
return fOrigin->magnitudeTo(*this);;
}
bool Point2D::isCollinear(const Point2D& aOther) const
{
if (fPosition.cross(aOther.fPosition) == 0)
{
return true;
}
return false;
}
// Check to see if the point is Clockwise or not
bool Point2D::isClockwise(const Point2D& aP0, const Point2D& aP2) const
{
double val = (fPosition.getY() - aP0.fPosition.getY()) * (aP2.fPosition.getX() - fPosition.getX()) -
(fPosition.getX() - aP0.fPosition.getX()) * (aP2.fPosition.getY() - fPosition.getY());
double val2 = fPosition.angleBetween(aP2.fPosition) - fPosition.angleBetween(aP0.fPosition);
if (val < 0 )
{
return false;
}
return true;
}
bool Point2D::operator<(const Point2D& aRHS) const
{
if (fPosition.getY() < aRHS.getY())
{
return true;
}
return false;
}
const Point2D& Point2D::getOrigin() const { return *fOrigin;}
std::ostream& operator<<(std::ostream& aOStream, const Point2D& aObject)
{
aOStream << aObject.fId << " : " << aObject.fPosition;
return aOStream;
}
std::istream& operator>>(std::istream& aIStream, Point2D& aObject)
{
aIStream >> aObject.fId >> aObject.fPosition;
return aIStream;
}
Point2DSet
#include "Point2DSet.h"
#include <fstream>
#include <stdexcept>
#include <algorithm>
void Point2DSet::add(const Point2D& aPoint)
{
fPoints.push_back(aPoint);
}
void Point2DSet::add(Point2D&& aPoint)
{
fPoints.push_back(aPoint);
}
void Point2DSet::removeLast()
{
fPoints.pop_back();
}
bool Point2DSet::doesNotTurnLeft(const Point2D& aPoint) const
{
return fPoints[size()-1].isClockwise(fPoints[size()-2],aPoint);
}
// Comparator function for Stable_sort
bool orderByCoordinates(const Point2D& aLeft, const Point2D& aRight)
{
return aLeft < aRight;
}
//Comparator function for Stable_sort
bool orderByPolarAngle(const Point2D& aLHS, const Point2D& aRHS)
{
if (aLHS.isCollinear(aRHS))
{
return aLHS.magnitude() > aRHS.magnitude();
}
return aLHS.direction() < aRHS.direction();
}
void Point2DSet::populate(const std::string& aFileName)
{
std::ifstream INPUT(aFileName);
//std::ifstream INPUT("Pointers.txt");
std::string id;
double x;
double y;
while (INPUT >> id >> x >> y)
{
Point2D z(id, x, y);
add(z);
}
INPUT.close();
}
void Point2DSet::buildConvexHull(Point2DSet& aConvexHull)
{
aConvexHull.clear();
sort(orderByCoordinates);
sort(orderByPolarAngle);
aConvexHull.add(fPoints[0]); // Origin (Smallest y-coordinate)
aConvexHull.add(fPoints[1]); //
//aConvexHull.add(fPoints[2]);
if (fPoints[2].isCollinear(fPoints[1])) {
aConvexHull.add(fPoints[2]);
}
//*/
for(size_t i = 3; i < size(); i++)
{
if (fPoints[i - 1].isCollinear(fPoints[i]))
{
continue; //i++;
}
if(aConvexHull.doesNotTurnLeft(fPoints[i]))
{
aConvexHull.removeLast();
}
aConvexHull.add(fPoints[i]);
}//*/
}
size_t Point2DSet::size() const
{
return fPoints.size();
}
void Point2DSet::clear()
{
fPoints.clear();
}
void Point2DSet::sort(Comparator aComparator)
{
stable_sort(fPoints.begin(), fPoints.end(), aComparator);
}
const Point2D& Point2DSet::operator[](size_t aIndex) const
{
return fPoints[aIndex];
}
Point2DSet::Iterator Point2DSet::begin() const
{
return fPoints.begin();
}
Point2DSet::Iterator Point2DSet::end() const
{
return fPoints.end();
}
Any other improvements are warmly welcome. Thank You!
There was a few issues in your code.
Let's start with Vector2D::direction(), you should use atan2, here the explanation why. After that we will be able to correctly sort the points.
Now the main algorithm. After a few changes it looks:
aConvexHull.clear();
// Get points with bigger magnitude first.
sort(orderByMagnitudeDescending);
sort(orderByPolarAngle);
// We want to have the lowest point as the first element.
rotatePointsByLowest();
aConvexHull.add(fPoints[0]); // Origin (Smallest y-coordinate)
aConvexHull.add(fPoints[1]);
for(size_t i = 2; i < size(); i++)
{
if (fPoints[i - 1].isCollinear(fPoints[i]))
{
continue; //i++;
}
// There should be a loop instead of an if statement.
while (aConvexHull.fPoints.size() > 2 && aConvexHull.doesNotTurnLeft(fPoints[i]))
{
aConvexHull.removeLast();
}
aConvexHull.add(fPoints[i]);
}//*/
The algorithm requires to find the lowest point and then traverse the rest of points according to their angle. I added a helper function Point2DSet::rotatePointsByLowest:
void Point2DSet::rotatePointsByLowest() {
auto lowestPoint = fPoints.begin();
for (auto iterator = fPoints.begin() + 1;iterator != fPoints.end(); iterator++) {
if (iterator->fPosition.fY < lowestPoint->fPosition.fY) {
lowestPoint = iterator;
} else if ((iterator->fPosition.fY == lowestPoint->fPosition.fY) && (iterator->fPosition.fX < lowestPoint->fPosition.fX)) {
lowestPoint = iterator;
}
}
std::rotate(fPoints.begin(), lowestPoint, fPoints.end());
}
There are more improvements that should be applied but I wanted to keep the changes minimal to show the issues causing the incorrect result.
Link for testing your project: https://onlinegdb.com/_ZXmQF2vJ

Change values of struct with overloaded operator []

I'm trying to make a custom vector data structure. It will always contain 4 float values. I want to make it accessible via its attributes names, but also through operator [ ], so it could be also accessed with index like e.g. array. It should be editable through operator [ ], like vector or array.
I've done so far this:
struct vec4
{
float* x;
float* y;
float* z;
float* w;
vec4() : data(4, 0.0f) { Init(); }
vec4(float x, float y, float z, float w)
{
data = std::vector<float>{ x, y, z, w };
Init();
}
float* operator[](int i) const
{
switch (i)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: __debugbreak();
}
}
private:
std::vector<float> data;
void Init()
{
std::vector<float>::iterator start = data.begin();
this->x = &(*++start);
this->y = &(*++start);
this->z = &(*++start);
this->w = &(*start);
}
};
Is there any more elegant way how to solve this problem?
Having individual members and then using a switch statement is pretty much the way to do this. You don't need the vector though and can write the class like
struct vec4
{
float x = 0;
float y = 0;
float z = 0;
float w = 0;
vec4() = default;
vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
const float& operator[](int i) const
{
switch (i)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: __debugbreak();
}
}
float& operator[](int i)
{
// this is safe because we are in a non-const function, so this cant point to a const object
return const_cast<float&>(static_cast<const vec4&>(*this)[i]);
}
};
How about
struct vec4
{
vec4() : vec4(0, 0, 0, 0) {}
vec4(float x, float y, float z, float w) : data{ x, y, z, w } {}
float operator[](int i) const { return data[i]; }
float& operator[](int i) { return data[i]; }
float at(int i) const { return data.at(i); }
float& at(int i) { return data.at(i); }
float x() const { return data[0]; }
float y() const { return data[1]; }
float z() const { return data[2]; }
float w() const { return data[3]; }
float& x() { return data[0]; }
float& y() { return data[1]; }
float& z() { return data[2]; }
float& w() { return data[3]; }
private:
std::array<float, 4> data;
};

Inheritance of operator overloads

I'm having an issue with the inheritance of my operator overloading functions in my derived class.
#pragma once
#include "Math.hpp"
using namespace Math;
class Euler
{
public:
float x, y, z;
Euler();
Euler(const float &, const float &, const float &);
float &operator[](const char &) const;
Euler &operator=(const Euler &);
Euler &operator+=(const Euler &);
Euler &operator-=(const Euler &);
Euler &operator*=(const float &);
Euler &operator/=(const float &);
Euler operator+(const Euler &) const;
Euler operator-(const Euler &) const;
Euler operator*(const float &) const;
Euler operator/(const float &) const;
bool operator==(const Euler &) const;
bool operator!=(const Euler &) const;
bool IsValid() const;
void Clear();
};
inline Euler::Euler()
{
x = y = z = 0.f;
}
inline Euler::Euler(const float &_x, const float &_y, const float &_z)
{
x = _x;
y = _y;
z = _z;
}
inline float &Euler::operator[](const char &c) const
{
return ((float *)this)[c];
}
inline Euler &Euler::operator=(const Euler &e)
{
x = e.x;
y = e.y;
z = e.z;
return *this;
}
inline Euler &Euler::operator+=(const Euler &e)
{
x += e.x;
y += e.y;
z += e.z;
return *this;
}
inline Euler &Euler::operator-=(const Euler &e)
{
x -= e.x;
y -= e.y;
z -= e.z;
return *this;
}
inline Euler &Euler::operator*=(const float &e)
{
x *= e;
y *= e;
z *= e;
return *this;
}
inline Euler &Euler::operator/=(const float &e)
{
x /= e + M_FLT_EPSILON;
y /= e + M_FLT_EPSILON;
z /= e + M_FLT_EPSILON;
return *this;
}
inline Euler Euler::operator+(const Euler &e) const
{
return Euler(x + e.x, y + e.y, z + e.z);
}
inline Euler Euler::operator-(const Euler &e) const
{
return Euler(x - e.x, y - e.y, z - e.z);
}
inline Euler Euler::operator*(const float &f) const
{
return Euler(x * f, y * f, z * f);
}
inline Euler Euler::operator/(const float &f) const
{
return Euler(x / (f + M_FLT_EPSILON), y / (f + M_FLT_EPSILON), z / (f + M_FLT_EPSILON));
}
inline bool Euler::operator==(const Euler &e) const
{
return e.x == x && e.y == y && e.z == z;
}
inline bool Euler::operator!=(const Euler &e) const
{
return e.x != x || e.y != y || e.z != z;
}
inline bool Euler::IsValid() const
{
using namespace std;
return isfinite(x) && isfinite(y) && isfinite(z);
}
inline void Euler::Clear()
{
x = y = z = 0.f;
}
class Vector : public Euler
{
public:
using Euler::Euler;
void Rotate(const Angle &);
void Rotate2D(const float &);
float Length() const;
float LengthSqr() const;
float Length2D() const;
float Length2DSqr() const;
float DistTo(const Vector &) const;
float DistToSqr(const Vector &) const;
};
inline float Vector::Length() const
{
return Sqrt((x * x) + (y * y) + (z * z));
}
inline float Vector::LengthSqr() const
{
return (x * x) + (y * y) + (z * z);
}
inline float Vector::Length2D() const
{
return Sqrt((x * x) + (y * y));
}
inline float Vector::Length2DSqr() const
{
return (x * x) + (y * y);
}
inline float Vector::DistTo(const Vector &v) const
{
return (*this - v).Length();
}
inline float Vector::DistToSqr(const Vector &v) const
{
return (*this - v).LengthSqr();
}
The code shown below contains an error in the distto and DistToSqr function wherein it calculates (*this - v) as the superclass Euler and therefore cannot find the length function.
I was wondering why this would be as this code compiles on my laptop and not my desktop.
I'd be grateful if anyone could show me why this code doesn't work and what is the best course in fixing it.
It seems like doing this is a plausible way to fix it.
inline float Vector::DistTo(const Vector &v) const
{
Vector tmp = (*this - v);
return tmp.Length();
}
Still wondering if this is the best option to fix it though.
I'm going to assume that it is likely that Euler is always used as a base type of a more specific type? If so, you can might be able to solve this using the CRTP pattern. Here is a subset of your code in this style:
#include <cmath>
template <typename Derived>
class Euler
{
public:
float x, y, z;
Euler();
Euler(const float &, const float &, const float &);
template <typename T>
Derived & operator-=(Euler<T> const &);
template <typename T>
Derived operator-(Euler<T> const &) const;
private:
Derived & derived() const {
return static_cast<Derived &>(*this);
}
Derived const & derived() {
return static_cast<Derived const &>(*this);
}
};
template <typename Derived>
Euler<Derived>::Euler() : x(0), y(0), z(0)
{}
template <typename Derived>
Euler<Derived>::Euler(const float &_x, const float &_y, const float &_z)
: x(_x), y(_y), z(_z)
{}
template <typename Derived>
template <typename T>
Derived & Euler<Derived>::operator-=(Euler<T> const & e)
{
x -= e.x;
y -= e.y;
z -= e.z;
return derived();
}
template <typename Derived>
template <typename T>
Derived Euler<Derived>::operator-(Euler<T> const & e) const
{
return Derived(x - e.x, y - e.y, z - e.z);
}
class Vector : public Euler<Vector>
{
public:
using Euler<Vector>::Euler;
float Length() const;
float DistTo(const Vector &) const;
};
inline float Vector::Length() const
{
return std::sqrt(x * x + y * y + z * z);
}
inline float Vector::DistTo(const Vector & v) const
{
return (*this - v).Length();
}

c++ how to conserve vector items and double-precision

I'm trying to fill a vector of an object Point 3D. My app read a csv file to load the vector by the three cordinate x, y, z. I use the type float.
This is my code.
main.cpp
int main(int argc, char** argv) {
char *theFileName = "file.csv"; //[100];
vector<Point> v = getPointCloud(theFileName);
for (int i = 0; i < v.size(); ++i) {
v.at(i).print(cout);
}
}
getPointCloud
vector<Point> getPointCloud(char *fileName) {
string line;
string token;
vector<Point> v;
double tab[3];
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
cpt++;
}
Point p(tab[0], tab[1], tab[2]);
p.print(cout); <-- the display works
p.setColor(255, 0, 0);
v.push_back(p);
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return v;
}
I have two problems:
1 - when I try to display points in the main method, I found that the three coordinates are null ( == 0) but in the displaying in the getPointCloud method works very well.
2 - Can someone give a simple method to conserve my coordinates without loss precision after mathematical operations. I have searched in the net but I don't understand haw to solve it. I'm newbie with c++.
Point.h
#ifndef POINT_H
#define POINT_H
#include <math.h>
#include <iostream>
class Point {
protected:
float x;
float y;
float z;
// color RGB
float r;
float g;
float b;
public:
// Constructors
Point();
// Point(const Point& orig);
Point(std::ostream &strm);
Point(float x, float y, float z);
Point(const Point& orig);
virtual ~Point();
//getters
float getX() const {
return this->x;
}
float getY() const {
return this->y;
}
float getZ() const {
return this->z;
}
float getR() const {
return this->r;
}
float getG() const {
return this->g;
}
float getB() const {
return this->b;
}
//setters
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
void setZ(float z) {
this->z = z;
}
void setR(float r) {
this->r = r;
}
void setG(float g) {
this->g = g;
}
void setB(float b) {
this->b = b;
}
void setColor(float r, float g, float b) {
this->r = r;
this->g = g;
this->b = b;
}
/**
* Print the point
* #param strm
*/
void print(std::ostream &strm);
//Other methods
float dist2D(Point &other);
float dist3D(Point &other);
Point swap(Point p);
// Point operator-(const Point &other) const;
};
#endif /* POINT_H */
Point.cpp
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
#include "Point.h"
Point::Point(const Point& orig) {
}
Point::Point(ostream &strm) {
strm << "Type the abscissa: ", cin >> this->x;
strm << "Type the ordinate: ", cin >> this->y;
strm << "Type the applicate: ", cin >> this->z;
}
Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
// The default point color is blue
this->r = 0;
this->g = 0;
this->b = 255;
}
/**
* Destructor
*/
Point::~Point() {
}
//Other methods
float Point::dist2D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
return sqrt(xd * xd + yd * yd);
}
float Point::dist3D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
float zd = z - other.z;
return sqrt(xd * xd + yd * yd + zd * zd);
}
Point Point::swap(Point p) {
Point aux(x, y, z);
x = p.x;
y = p.y;
z = p.z;
return aux;
}
//Point Point::operator-(const Point &other) const {
// return Point(other.getX() - this->x, other.getY() - this->y, other.getZ() - this->z);
//}
void Point::print(ostream &strm) {
strm << "Point(" << this->x << "," << y << "," << z << ")" << endl;
}
Thanks in advance.
Point::Point(const Point& orig) {
}
is incorrect.
It does not copy data from orig to *this
Please copy each of the member in this constructor.
This would look like this:
Point::Point(const Point& orig) {
x = orig.x ;
y = orig.y ;
x = orig.z ;
r = orig.r ;
g = orig.g ;
b = orig.b ;
}