FPoint.hpp 8.95 KB
Newer Older
1
// ===================================================================================
COULAUD Olivier's avatar
COULAUD Olivier committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
15
// ===================================================================================
COULAUD Olivier's avatar
COULAUD Olivier committed
16
//
BRAMAS Berenger's avatar
BRAMAS Berenger committed
17 18
#ifndef FPOINT_HPP
#define FPOINT_HPP
19

20 21 22

// To get memcpy
#include <cstring>
berenger-bramas's avatar
berenger-bramas committed
23
#include <iostream>
24

COULAUD Olivier's avatar
COULAUD Olivier committed
25
#include "FMath.hpp"
26 27 28 29
#include "FGlobal.hpp"

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
BRAMAS Berenger's avatar
BRAMAS Berenger committed
30
* @class FPoint
31 32 33 34 35
* Please read the license
*
* This class is a 3D vector. It can be used as a position
* or as a 3d forces vector etc.
*/
BRAMAS Berenger's avatar
BRAMAS Berenger committed
36
class FPoint {
37
private:
BRAMAS Berenger's avatar
BRAMAS Berenger committed
38
    FReal data[3]; //< all positions x y z
39 40

public:	
41
    /** Default constructor (sets position to 0/0/0) */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
42
    FPoint(){
43
        data[0] = data[1] = data[2] = FReal(0);
44 45
    }

46 47 48 49 50 51 52
    /** Constructor from an array */
    explicit FPoint(const FReal inPosition[3]){
        data[0] = inPosition[0];
        data[1] = inPosition[1];
        data[2] = inPosition[2];
    }

53
    /** Constructor from values */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
54
    explicit FPoint(const FReal inX,const FReal inY,const FReal inZ){
55 56 57
        data[0] = inX;
        data[1] = inY;
        data[2] = inZ;
58 59 60
    }

    /**
61
 * Copdata[1] constructor
62 63
 * @param other the source class to copy
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
64
    FPoint(const FPoint& other) {
65 66 67
        data[0] = other.data[0];
        data[1] = other.data[1];
        data[2] = other.data[2];
68 69 70
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
71
        * Assignment operator
72 73
        * @param other the source class to copy
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
74
    FPoint(const FPoint& other, const FReal addset) {
75 76 77
        data[0] = other.data[0] + addset;
        data[1] = other.data[1] + addset;
        data[2] = other.data[2] + addset;
78 79 80
    }

    /**
81
 * Copdata[1] constructor
82 83 84
 * @param other the source class to copy
 * @return this a reference to the current class
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
85
    FPoint& operator=(const FPoint& other){
86 87 88
        this->data[0] = other.data[0];
        this->data[1] = other.data[1];
        this->data[2] = other.data[2];
89 90 91 92 93 94 95 96 97
        return *this;
    }

    /**
 * Position setter
 * @param other the source class to copy
 * @return this a reference to the current class
 */
    void setPosition(const FReal inX,const FReal inY,const FReal inZ){
98 99 100
        this->data[0] = inX;
        this->data[1] = inY;
        this->data[2] = inZ;
101 102 103 104
    }

    /**
 * Get x
105
 * @return this->data[0]
106 107
 */
    FReal getX() const{
108
        return this->data[0];
109 110 111 112
    }

    /**
 * Get y
113
 * @return this->data[1]
114 115
 */
    FReal getY() const{
116
        return this->data[1];
117 118 119 120
    }

    /**
 * Get z
121
 * @return this->data[2]
122 123
 */
    FReal getZ() const{
124
        return this->data[2];
125 126 127 128 129 130 131
    }

    /**
 * Set x
 * @param the new x
 */
    void setX(const FReal inX){
132
        this->data[0] = inX;
133 134 135 136 137 138 139
    }

    /**
 * Set y
 * @param the new y
 */
    void setY(const FReal inY){
140
        this->data[1] = inY;
141 142 143 144 145 146 147
    }

    /**
 * Set z
 * @param the new z
 */
    void setZ(const FReal inZ){
148
        this->data[2] = inZ;
149 150 151
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
152 153
 * Add to the x-dimension the inX value
 * @param  inXthe increment in x
154 155
 */
    void incX(const FReal inX){
156
        this->data[0] += inX;
157 158 159
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
160 161
 *  Add to the y-dimension the inY value
 * @param  in<<<<<<y the increment in y
162 163
 */
    void incY(const FReal inY){
164
        this->data[1] += inY;
165 166 167
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
168 169
 *  Add to z-dimension the inZ value
 * @param inZ the increment in z
170 171
 */
    void incZ(const FReal inZ){
172
        this->data[2] += inZ;
173
    }
COULAUD Olivier's avatar
COULAUD Olivier committed
174 175 176 177 178 179 180
    /**
      * Get a pointer on the coordinate of FPoint
      * @return the data value array
     */
    FReal * getDataValue(){
        return this->data ;
    }
COULAUD Olivier's avatar
COULAUD Olivier committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    /**
      *Compute the distance to the origin
      * @return the norm of the Fpoint
      */
      FReal norm() const {
          return FMath::Sqrt(this->data[0]*this->data[0]+this->data[1]*this->data[1]
                          +this->data[2]*this->data[2]) ;
      }

      /**
        *Compute the distance to the origin
        * @return the square norm of the Fpoint
        */
        FReal norm2() const {
            return (this->data[0]*this->data[0]+this->data[1]*this->data[1]
                            +this->data[2]*this->data[2]) ;
        }
198 199 200 201 202 203

    /**
 * Subtract to all dim the inValue
 * @param inValue the value to substract
 * @return the current object after being subtracted
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
204
    FPoint& operator-=(const FReal inValue){
205 206 207
        this->data[0] -= inValue;
        this->data[1] -= inValue;
        this->data[2] -= inValue;
208 209 210 211 212
        return *this;
    }

    /**
 * Affect to all dim the inValue
COULAUD Olivier's avatar
COULAUD Olivier committed
213
 * @param inValue the value to affect
214 215
 * @return the current object after being affected
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
216
    FPoint& operator+=(const FReal inValue){
217 218 219
        this->data[0] += inValue;
        this->data[1] += inValue;
        this->data[2] += inValue;
220 221 222 223
        return *this;
    }

    /**
224 225 226 227
        * Subtract to all dim the other position
        * @param other the value to substract
        * @return the current object after being subtracted
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
228
    FPoint& operator-=(const FPoint& other){
229 230 231
        this->data[0] -= other.data[0];
        this->data[1] -= other.data[1];
        this->data[2] -= other.data[2];
232 233 234 235
        return *this;
    }

    /**
236 237 238 239
        * Affect to all dim the other position
        * @param other the value to afect
        * @return the current object after being affected
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
240
    FPoint& operator+=(const FPoint& other){
241 242 243
        this->data[0] += other.data[0];
        this->data[1] += other.data[1];
        this->data[2] += other.data[2];
244 245 246 247
        return *this;
    }

    /**
248 249 250 251
        * Affect to all dim the other position
        * @param other the value to afect
        * @return the current object after being affected
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
252
    FPoint& operator*=(const FReal value){
253 254 255
        this->data[0] *= value;
        this->data[1] *= value;
        this->data[2] *= value;
256 257 258 259
        return *this;
    }

    /**
260 261 262 263 264 265
        * Operator F3Position minus FReal
        * This substract inValue to all dimensions of the inPosition
        * @param inPosition the position to compute
        * @param inValue the value to decrease/substract position
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
266 267
    friend inline FPoint operator-(const FPoint& inPosition, const FReal inValue){
        return FPoint(inPosition, -inValue);
268
    }
269

270
    /**
271 272 273 274 275 276
        * Operator F3Position plus FReal
        * This affect from inValue all dimensions of the inPosition
        * @param inPosition the position to compute
        * @param inValue the value to increase/affect position
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
277 278
    friend inline FPoint operator+(const FPoint& inPosition, const FReal inValue){
        return FPoint(inPosition, inValue);
279
    }
280

281
    /**
282 283 284 285 286 287
        * Operator F3Position minus F3Position
        * This substract one from anther
        * @param inPosition the position to reduce
        * @param inOther the position to decrease/substract inPosition
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
288 289
    friend inline FPoint operator-(const FPoint& inPosition, const FPoint& inOther){
        return FPoint(inPosition.data[0] - inOther.data[0], inPosition.data[1] - inOther.data[1], inPosition.data[2] - inOther.data[2]);
290
    }
291

292
    /**
293 294 295 296 297 298
        * Operator F3Position plus F3Position
        * This substract one from anther
        * @param inPosition the position to reduce
        * @param inOther the position to increase inPosition
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
299 300
    friend inline FPoint operator+(const FPoint& inPosition, const FPoint& inOther){
        return FPoint(inPosition.data[0] + inOther.data[0], inPosition.data[1] + inOther.data[1], inPosition.data[2] + inOther.data[2]);
301
    }
messner's avatar
messner committed
302

berenger-bramas's avatar
berenger-bramas committed
303
    /**
BRAMAS Berenger's avatar
BRAMAS Berenger committed
304
     * Operator stream FPoint to std::ostream
305
     * This can be used to simpldata[1] write out a position
berenger-bramas's avatar
berenger-bramas committed
306 307 308 309
     * @param[in,out] output where to write the position
     * @param[in] inPosition the position to write out
     * @return the output for multiple << operators
     */
310 311
    template <class StreamClass>
    friend StreamClass& operator<<(StreamClass& output, const FPoint& inPosition){
berenger-bramas's avatar
berenger-bramas committed
312 313 314
        output << "(" <<  inPosition.getX() << ", " << inPosition.getY() << ", " << inPosition.getZ() <<")";
        return output;  // for multiple << operators.
    }
315

316 317 318
    /** Save current object */    
    template <class BufferWriterClass>
    void save(BufferWriterClass& buffer) const {
319
        buffer << data[0] << data[1] << data[2];
320 321
    }
    /** Retrieve current object */
322 323
    template <class BufferReaderClass>
    void restore(BufferReaderClass& buffer) {
324
        buffer >> data[0] >> data[1] >> data[2];
325
    }
326 327 328
};


329

330

BRAMAS Berenger's avatar
BRAMAS Berenger committed
331
#endif //FPOINT_HPP
332

333