binding/v8/jsi/geometry/Point.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Point.cpp
22 
23  \brief Utility functions to register the Point class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../core/logger/Logger.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../geometry/Point.h"
30 #include "../../../../geometry/PointM.h"
31 #include "../../../../geometry/PointZ.h"
32 #include "../../../../geometry/PointZM.h"
33 #include "../../common/Utils.h"
34 #include "Geometry.h"
35 
36 ::v8::Handle<::v8::Value> Point_GetX(const ::v8::Arguments& args)
37 {
38  ::v8::HandleScope hs;
39 
40  if(args.Holder().IsEmpty())
41  return ::v8::ThrowException(::v8::String::New("Point getX method must use object notation!"));
42 
43  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
44 
45  return ::v8::Number::New(pt->getX());
46 }
47 
48 ::v8::Handle<::v8::Value> Point_SetX(const ::v8::Arguments& args)
49 {
50  ::v8::HandleScope hs;
51 
52  if(args.Holder().IsEmpty())
53  return ::v8::ThrowException(::v8::String::New("Point setX method must use object notation!"));
54 
55  if((args.Length() != 1) || !args[0]->IsNumber())
56  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong type in Point setX method!"));
57 
58  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
59 
60  pt->setX(args[0]->ToNumber()->Value());
61 
62  return ::v8::Undefined();
63 }
64 
65 ::v8::Handle<::v8::Value> Point_GetY(const ::v8::Arguments& args)
66 {
67  ::v8::HandleScope hs;
68 
69  if(args.Holder().IsEmpty())
70  return ::v8::ThrowException(::v8::String::New("Point getY method must use object notation!"));
71 
72  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
73 
74  return ::v8::Number::New(pt->getY());
75 }
76 
77 ::v8::Handle<::v8::Value> Point_SetY(const ::v8::Arguments& args)
78 {
79  ::v8::HandleScope hs;
80 
81  if(args.Holder().IsEmpty())
82  return ::v8::ThrowException(::v8::String::New("Point setY method must use object notation!"));
83 
84  if((args.Length() != 1) || !args[0]->IsNumber())
85  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong type in Point setX method!"));
86 
87  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
88 
89  pt->setY(args[0]->ToNumber()->Value());
90 
91  return ::v8::Undefined();
92 }
93 
94 ::v8::Handle<::v8::Value> Point_GetZ(const ::v8::Arguments& args)
95 {
96  ::v8::HandleScope hs;
97 
98  if(args.Holder().IsEmpty())
99  return ::v8::ThrowException(::v8::String::New("Point getZ method must use object notation!"));
100 
101  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
102 
103  return ::v8::Number::New(pt->getZ());
104 }
105 
106 ::v8::Handle<::v8::Value> Point_SetZ(const ::v8::Arguments& args)
107 {
108  ::v8::HandleScope hs;
109 
110  if(args.Holder().IsEmpty())
111  return ::v8::ThrowException(::v8::String::New("Point setZ method must use object notation!"));
112 
113  if((args.Length() != 1) || !args[0]->IsNumber())
114  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong type in Point setZ method!"));
115 
116  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
117 
118  pt->setZ(args[0]->ToNumber()->Value());
119 
120  return ::v8::Undefined();
121 }
122 
123 ::v8::Handle<::v8::Value> Point_GetM(const ::v8::Arguments& args)
124 {
125  ::v8::HandleScope hs;
126 
127  if(args.Holder().IsEmpty())
128  return ::v8::ThrowException(::v8::String::New("Point getX method must use object notation!"));
129 
130  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
131 
132  return ::v8::Number::New(pt->getM());
133 }
134 
135 ::v8::Handle<::v8::Value> Point_SetM(const ::v8::Arguments& args)
136 {
137  ::v8::HandleScope hs;
138 
139  if(args.Holder().IsEmpty())
140  return ::v8::ThrowException(::v8::String::New("Point setM method must use object notation!"));
141 
142  if((args.Length() != 1) || !args[0]->IsNumber())
143  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong type in Point setM method!"));
144 
145  te::gm::Point* pt = te::v8::common::Unwrap<te::gm::Point>(args.Holder());
146 
147  pt->setM(args[0]->ToNumber()->Value());
148 
149  return ::v8::Undefined();
150 }
151 
152 ::v8::Handle<::v8::Value> Point_Constructor(const ::v8::Arguments& args)
153 {
154  ::v8::HandleScope hs;
155 
156  if(!args.IsConstructCall())
157  return ::v8::ThrowException(::v8::String::New("In order to create a point you need call its constructor like: var mypt = new TePoint(1.0, 2.0, 4326)."));
158 
159  if(args.Holder().IsEmpty())
160  return ::v8::ThrowException(::v8::String::New("Point constructor must use object notation!"));
161 
162  double x = args[0]->ToNumber()->Value();
163  double y = args[1]->ToNumber()->Value();
164  int srid = args[2]->ToInt32()->Int32Value();
165 
166  std::auto_ptr<te::gm::Point> pt(new te::gm::Point(x, y, srid));
167 
168  ::v8::Local<::v8::Object> jpt = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
169 
170  pt.release();
171 
172  return hs.Close(jpt);
173 }
174 
175 ::v8::Handle<::v8::Value> PointZ_Constructor(const ::v8::Arguments& args)
176 {
177  ::v8::HandleScope hs;
178 
179  if(!args.IsConstructCall())
180  return ::v8::ThrowException(::v8::String::New("In order to create a point you need call its constructor like: var mypt = new TePointZ(1.0, 2.0, 3.0, 4326)."));
181 
182  if(args.Holder().IsEmpty())
183  return ::v8::ThrowException(::v8::String::New("Point constructor must use object notation!"));
184 
185  double x = args[0]->ToNumber()->Value();
186  double y = args[1]->ToNumber()->Value();
187  double z = args[2]->ToNumber()->Value();
188  int srid = args[3]->ToInt32()->Int32Value();
189 
190  std::auto_ptr<te::gm::PointZ> pt(new te::gm::PointZ(x, y, z, srid));
191 
192  ::v8::Local<::v8::Object> jpt = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
193 
194  pt.release();
195 
196  return hs.Close(jpt);
197 }
198 
199 ::v8::Handle<::v8::Value> PointM_Constructor(const ::v8::Arguments& args)
200 {
201  ::v8::HandleScope hs;
202 
203  if(!args.IsConstructCall())
204  return ::v8::ThrowException(::v8::String::New("In order to create a point you need call its constructor like: var mypt = new TePointM(1.0, 2.0, 3.0, 4326)."));
205 
206  if(args.Holder().IsEmpty())
207  return ::v8::ThrowException(::v8::String::New("Point constructor must use object notation!"));
208 
209  double x = args[0]->ToNumber()->Value();
210  double y = args[1]->ToNumber()->Value();
211  double m = args[2]->ToNumber()->Value();
212  int srid = args[3]->ToInt32()->Int32Value();
213 
214  std::auto_ptr<te::gm::PointM> pt(new te::gm::PointM(x, y, m, srid));
215 
216  ::v8::Local<::v8::Object> jpt = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
217 
218  pt.release();
219 
220  return hs.Close(jpt);
221 }
222 
223 ::v8::Handle<::v8::Value> PointZM_Constructor(const ::v8::Arguments& args)
224 {
225  ::v8::HandleScope hs;
226 
227  if(!args.IsConstructCall())
228  return ::v8::ThrowException(::v8::String::New("In order to create a point you need call its constructor like: var mypt = new TePointZM(1.0, 2.0, 3.0, 4.0, 4326)."));
229 
230  if(args.Holder().IsEmpty())
231  return ::v8::ThrowException(::v8::String::New("Point constructor must use object notation!"));
232 
233  double x = args[0]->ToNumber()->Value();
234  double y = args[1]->ToNumber()->Value();
235  double z = args[2]->ToNumber()->Value();
236  double m = args[3]->ToNumber()->Value();
237  int srid = args[4]->ToInt32()->Int32Value();
238 
239  std::auto_ptr<te::gm::PointZM> pt(new te::gm::PointZM(x, y, z, m, srid));
240 
241  ::v8::Local<::v8::Object> jpt = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
242 
243  pt.release();
244 
245  return hs.Close(jpt);
246 }
247 
248 void te::v8::jsi::RegisterPoint(::v8::Local<::v8::Object>& global)
249 {
250  ::v8::HandleScope hs;
251 
252  ::v8::Local<::v8::FunctionTemplate> jspt = ::v8::FunctionTemplate::New(Point_Constructor);
253  ::v8::Local<::v8::FunctionTemplate> jsptz = ::v8::FunctionTemplate::New(PointZ_Constructor);
254  ::v8::Local<::v8::FunctionTemplate> jsptm = ::v8::FunctionTemplate::New(PointM_Constructor);
255  ::v8::Local<::v8::FunctionTemplate> jsptzm = ::v8::FunctionTemplate::New(PointZM_Constructor);
256 
257  global->Set(::v8::String::New("TePoint"), jspt->GetFunction());
258  global->Set(::v8::String::New("TePointZ"), jsptz->GetFunction());
259  global->Set(::v8::String::New("TePointM"), jsptm->GetFunction());
260  global->Set(::v8::String::New("TePointZM"), jsptzm->GetFunction());
261 }
262 
263 static ::v8::Persistent<::v8::FunctionTemplate> sg_point_template;
264 
265 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetPointTemplate()
266 {
267  if(sg_point_template.IsEmpty())
268  {
269  ::v8::Persistent<::v8::FunctionTemplate>& geomTpl = GetGeometryTemplate();
270  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
271  result->Inherit(geomTpl);
272 
273  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
274 
275  prototype->Set(::v8::String::NewSymbol("getX"), ::v8::FunctionTemplate::New(Point_GetX));
276  prototype->Set(::v8::String::NewSymbol("setX"), ::v8::FunctionTemplate::New(Point_SetX));
277  prototype->Set(::v8::String::NewSymbol("getY"), ::v8::FunctionTemplate::New(Point_GetY));
278  prototype->Set(::v8::String::NewSymbol("setY"), ::v8::FunctionTemplate::New(Point_SetY));
279  prototype->Set(::v8::String::NewSymbol("getZ"), ::v8::FunctionTemplate::New(Point_GetZ));
280  prototype->Set(::v8::String::NewSymbol("setZ"), ::v8::FunctionTemplate::New(Point_SetZ));
281  prototype->Set(::v8::String::NewSymbol("getM"), ::v8::FunctionTemplate::New(Point_GetM));
282  prototype->Set(::v8::String::NewSymbol("setM"), ::v8::FunctionTemplate::New(Point_SetM));
283 
284  sg_point_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
285  }
286 
287  return sg_point_template;
288 }
289 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
::v8::Persistent<::v8::FunctionTemplate > & GetPointTemplate()
It returns a reference to the persistent template of a Point object.
::v8::Handle<::v8::Value > Point_SetX(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Point_GetX(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Point_GetM(const ::v8::Arguments &args)
void RegisterPoint(::v8::Local<::v8::Object > &global)
It register the Point class.
::v8::Handle<::v8::Value > Point_GetY(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > PointM_Constructor(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Point_GetZ(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Point_Constructor(const ::v8::Arguments &args)
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:152
A point with x and y coordinate values.
Definition: Point.h:50
void setM(const double &m)
It sets the Point m-coordinate value.
Definition: Point.h:187
const double & getZ() const
It returns the Point z-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:166
::v8::Persistent<::v8::FunctionTemplate > & GetGeometryTemplate()
It returns a reference to the persistent template of a Geometry object.
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:145
::v8::Handle<::v8::Value > Point_SetZ(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Point_SetY(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > PointZM_Constructor(const ::v8::Arguments &args)
const double & getM() const
It returns the Point m-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:180
::v8::Handle<::v8::Value > Point_SetM(const ::v8::Arguments &args)
void setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:159
static::v8::Persistent<::v8::FunctionTemplate > sg_point_template
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:138
::v8::Handle<::v8::Value > PointZ_Constructor(const ::v8::Arguments &args)
void setZ(const double &z)
It sets the Point z-coordinate value.
Definition: Point.h:173