File: Point.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (250 lines) | stat: -rw-r--r-- 9,653 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// $Id$
//
//  Copyright (C) 2005 Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDBoost/python.h>

#include <RDBoost/Wrap.h>
#include <Geometry/point.h>
namespace python = boost::python;

namespace {
struct Point3D_pickle_suite : python::pickle_suite {
  static python::tuple getinitargs(RDGeom::Point3D const &pt) {
    return python::make_tuple(pt.x, pt.y, pt.z);
  }
};
struct Point2D_pickle_suite : python::pickle_suite {
  static python::tuple getinitargs(RDGeom::Point2D const &pt) {
    return python::make_tuple(pt.x, pt.y);
  }
};
struct PointND_pickle_suite : python::pickle_suite {
  static python::tuple getinitargs(RDGeom::PointND const &pt) {
    return python::make_tuple(pt.dimension());
  }
  static python::tuple getstate(RDGeom::PointND const &pt) {
    python::list res;
    for (unsigned int i = 0; i < pt.dimension(); ++i) {
      res.append(pt[i]);
    }
    return python::tuple(res);
  }
  static void setstate(RDGeom::PointND &pt, python::tuple state) {
    unsigned int sz = python::extract<unsigned int>(state.attr("__len__")());
    for (unsigned int i = 0; i < sz; ++i) {
      pt[i] = python::extract<double>(state[i]);
    }
  }
};
}

namespace RDGeom {

std::string Point3Ddoc =
    "A class to represent a three-dimensional point\n\
The x, y, and z coordinates can be read and written using either attributes\n\
(i.e. pt.x = 4) or indexing (i.e. pt[0] = 4).\n";
std::string Point2Ddoc = "A class to represent a two-dimensional point";
std::string PointNDdoc = "A class to represent an N-dimensional point";

double point3Ddist(const Point3D &pt1, const Point3D &pt2) {
  Point3D tpt(pt1);
  tpt -= pt2;
  return tpt.length();
}
double pointNdGetItem(const PointND &self, int idx) {
  if (idx >= static_cast<int>(self.dimension()) ||
      idx < -1 * static_cast<int>(self.dimension()))
    throw IndexErrorException(idx);
  if (idx < 0) idx = self.dimension() + idx;
  return self[idx];
}
double pointNdSetItem(PointND &self, int idx, double val) {
  if (idx >= static_cast<int>(self.dimension()) ||
      idx < -1 * static_cast<int>(self.dimension()))
    throw IndexErrorException(idx);
  if (idx < 0) idx = self.dimension() + idx;
  self[idx] = val;
  return val;
}

double point3dGetItem(const Point3D &self, int idx) {
  switch (idx) {
    case 0:
    case -3:
      return self.x;
      break;
    case 1:
    case -2:
      return self.y;
      break;
    case 2:
    case -1:
      return self.z;
      break;
    default:
      throw IndexErrorException(idx);
  }
}

double point2dGetItem(const Point2D &self, int idx) {
  switch (idx) {
    case 0:
    case -2:
      return self.x;
      break;
    case 1:
    case -1:
      return self.y;
      break;
    default:
      throw IndexErrorException(idx);
  }
}

struct Point_wrapper {
  static void wrap() {
    python::class_<Point3D>("Point3D", Point3Ddoc.c_str(),
                            python::init<>("Default Constructor"))
        .def(python::init<double, double, double>())
        .def_readwrite("x", &Point3D::x)
        .def_readwrite("y", &Point3D::y)
        .def_readwrite("z", &Point3D::z)
        .def("__getitem__", point3dGetItem)
        .def("__len__", &Point3D::dimension)
        .def("__iadd__", &Point3D::operator+=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Addition to another point")
        .def("__isub__", &Point3D::operator-=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Vector difference")
        .def(python::self - python::self)
        .def(python::self -= python::self)
        .def(python::self + python::self)
        .def(python::self += python::self)
        .def(python::self * double())
        .def(python::self / double())
        .def("__imul__", &Point3D::operator*=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar multiplication")
        .def("__idiv__", &Point3D::operator/=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar division")
        .def("Normalize", &Point3D::normalize,
             "Normalize the vector (using L2 norm)")
        .def("Length", &Point3D::length, "Length of the vector")
        .def("Distance", point3Ddist,
             "Distance from this point to another point")
        .def("LengthSq", &Point3D::lengthSq, "Square of the length")
        .def("DotProduct", &Point3D::dotProduct,
             "Dot product with another point")
        .def("AngleTo", &Point3D::angleTo,
             "determines the angle between a vector to this point (between 0 "
             "and PI)")
        .def("SignedAngleTo", &Point3D::signedAngleTo,
             "determines the signed angle between a vector to this point "
             "(between 0 and 2*PI)")
        .def("DirectionVector", &Point3D::directionVector,
             "return a normalized direction vector from this point to another")
        .def("CrossProduct", &Point3D::crossProduct,
             "Get the cross product between two points")

        .def_pickle(Point3D_pickle_suite());

    python::class_<Point2D>("Point2D", Point2Ddoc.c_str(),
                            python::init<>("Default Constructor"))
        .def(python::init<double, double>())
        .def_readwrite("x", &Point2D::x)
        .def_readwrite("y", &Point2D::y)
        .def("__getitem__", point2dGetItem)
        .def("__len__", &Point2D::dimension)
        .def(python::self - python::self)
        .def(python::self -= python::self)
        .def(python::self + python::self)
        .def(python::self += python::self)
        .def(python::self * double())
        .def(python::self / double())
        .def("__imul__", &Point2D::operator*=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar multiplication")
        .def("__idiv__", &Point2D::operator/=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar division")
        .def("Normalize", &Point2D::normalize,
             "Normalize the vector (using L2 norm)")
        .def("Length", &Point2D::length, "Length of the vector")
        .def("LengthSq", &Point2D::lengthSq, "Square of the length")
        .def("DotProduct", &Point2D::dotProduct,
             "Dot product with another point")
        .def("AngleTo", &Point2D::angleTo,
             "determines the angle between a vector to this point (between 0 "
             "and PI)")
        .def("SignedAngleTo", &Point2D::signedAngleTo,
             "determines the signed angle between a vector to this point "
             "(between 0 and 2*PI)")
        .def("DirectionVector", &Point2D::directionVector,
             "return a normalized direction vector from this point to another")

        .def_pickle(Point2D_pickle_suite());

    python::class_<PointND>("PointND", PointNDdoc.c_str(),
                            python::init<unsigned int>())
        .def("__getitem__", pointNdGetItem)
        .def("__setitem__", pointNdSetItem)
        .def("__len__", &PointND::dimension)
        .def("__iadd__", &PointND::operator+=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Addition to another point")
        .def("__isub__", &PointND::operator-=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Vector difference")
        .def(python::self - python::self)
        .def(python::self -= python::self)
        .def(python::self + python::self)
        .def(python::self += python::self)
        .def(python::self * double())
        .def(python::self / double())
        .def("__imul__", &PointND::operator*=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar multiplication")
        .def("__idiv__", &PointND::operator/=,
             python::return_value_policy<python::copy_non_const_reference>(),
             "Scalar division")
        .def("Normalize", &PointND::normalize,
             "Normalize the vector (using L2 norm)")
        .def("Length", &PointND::length, "Length of the vector")
        .def("Distance", point3Ddist,
             "Distance from this point to another point")
        .def("LengthSq", &PointND::lengthSq, "Square of the length")
        .def("DotProduct", &PointND::dotProduct,
             "Dot product with another point")
        .def("AngleTo", &PointND::angleTo,
             "determines the angle between a vector to this point (between 0 "
             "and PI)")
        .def("DirectionVector", &PointND::directionVector,
             "return a normalized direction vector from this point to another")
        //.def("SignedAngleTo", &PointND::signedAngleTo,
        //     "determines the signed angle between a vector to this point
        //     (between 0 and 2*PI)")
        //.def("CrossProduct", &PointND::crossProduct,
        //     "Get the cross product between two points")
        .def_pickle(PointND_pickle_suite());

    python::def(
        "ComputeDihedralAngle", computeDihedralAngle,
        "calculates the dihedral angle determined by four Point3D objects");
    python::def("ComputeSignedDihedralAngle", computeSignedDihedralAngle,
                "calculates the signed dihedral angle determined by four "
                "Point3D objects");
  }
};
}

void wrap_point() { RDGeom::Point_wrapper::wrap(); }