binding/v8/jsi/geometry/Curve.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 Curve.cpp
22 
23  \brief Utility functions to register the Curve class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/Curve.h"
28 #include "../../../../geometry/Point.h"
29 #include "../../common/Utils.h"
30 #include "Geometry.h"
31 
32 ::v8::Handle<::v8::Value> Curve_GetLength(const ::v8::Arguments& args)
33 {
34  ::v8::HandleScope hs;
35 
36  if(args.Holder().IsEmpty())
37  return ::v8::ThrowException(::v8::String::New("In order to use getLength method you must use object notation: \"len = obj.getLength();\""));
38 
39  te::gm::Curve* g = te::v8::common::Unwrap<te::gm::Curve>(args.Holder());
40 
41  if(g == 0)
42  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getLength method!"));
43 
44  double len = g->getLength();
45 
46  ::v8::Local<::v8::Number> jlen = ::v8::Number::New(len);
47 
48  return hs.Close(jlen);
49 }
50 
51 ::v8::Handle<::v8::Value> Curve_GetStartPoint(const ::v8::Arguments& args)
52 {
53  ::v8::HandleScope hs;
54 
55  if(args.Holder().IsEmpty())
56  return ::v8::ThrowException(::v8::String::New("In order to use getStartPoint method you must use object notation: \"pt = obj.getStartPoint();\""));
57 
58  te::gm::Curve* g = te::v8::common::Unwrap<te::gm::Curve>(args.Holder());
59 
60  if(g == 0)
61  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getStartPoint method!"));
62 
63  te::gm::Point* pt = g->getStartPoint();
64 
65  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(pt, te::v8::jsi::GetPointTemplate, true);
66 
67  return hs.Close(jsgeom);
68 }
69 
70 ::v8::Handle<::v8::Value> Curve_GetEndPoint(const ::v8::Arguments& args)
71 {
72  ::v8::HandleScope hs;
73 
74  if(args.Holder().IsEmpty())
75  return ::v8::ThrowException(::v8::String::New("In order to use getEndPoint method you must use object notation: \"pt = obj.getEndPoint();\""));
76 
77  te::gm::Curve* g = te::v8::common::Unwrap<te::gm::Curve>(args.Holder());
78 
79  if(g == 0)
80  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getEndPoint method!"));
81 
82  te::gm::Point* pt = g->getEndPoint();
83 
84  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(pt, te::v8::jsi::GetPointTemplate, true);
85 
86  return hs.Close(jsgeom);
87 }
88 
89 ::v8::Handle<::v8::Value> Curve_IsClosed(const ::v8::Arguments& args)
90 {
91  ::v8::HandleScope hs;
92 
93  if(args.Holder().IsEmpty())
94  return ::v8::ThrowException(::v8::String::New("In order to use isClosed method you must use object notation: \"isclosed = obj.isClosed();\""));
95 
96  te::gm::Curve* g = te::v8::common::Unwrap<te::gm::Curve>(args.Holder());
97 
98  if(g == 0)
99  return ::v8::ThrowException(::v8::String::New("Invalid geometry in isClosed method!"));
100 
101  bool is = g->isClosed();
102 
103  ::v8::Handle<::v8::Boolean> jis = ::v8::Boolean::New(is);
104 
105  return hs.Close(jis);
106 }
107 
108 static ::v8::Persistent<::v8::FunctionTemplate> sg_curve_template;
109 
110 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetCurveTemplate()
111 {
112  if(sg_curve_template.IsEmpty())
113  {
114  ::v8::Persistent<::v8::FunctionTemplate>& geomTpl = GetGeometryTemplate();
115  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
116  result->Inherit(geomTpl);
117 
118  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
119 
120  prototype->Set(::v8::String::NewSymbol("getLength"), ::v8::FunctionTemplate::New(Curve_GetLength));
121  prototype->Set(::v8::String::NewSymbol("getStartPoint"), ::v8::FunctionTemplate::New(Curve_GetStartPoint));
122  prototype->Set(::v8::String::NewSymbol("getEndPoint"), ::v8::FunctionTemplate::New(Curve_GetEndPoint));
123  prototype->Set(::v8::String::NewSymbol("isClosed"), ::v8::FunctionTemplate::New(Curve_IsClosed));
124 
125  sg_curve_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
126  }
127 
128  return sg_curve_template;
129 }
130 
::v8::Handle<::v8::Value > Curve_GetLength(const ::v8::Arguments &args)
::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.
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
Definition: Curve.h:58
::v8::Handle<::v8::Value > Curve_GetEndPoint(const ::v8::Arguments &args)
virtual double getLength() const
The length of this curve in the unit associated to its spatial reference system.
::v8::Handle<::v8::Value > Curve_GetStartPoint(const ::v8::Arguments &args)
virtual bool isClosed() const =0
It returns true if the curve is closed (startPoint = endPoint).
static::v8::Persistent<::v8::FunctionTemplate > sg_curve_template
virtual std::unique_ptr< Point > getStartPoint() const =0
It returns the curve start point.
::v8::Handle<::v8::Value > Curve_IsClosed(const ::v8::Arguments &args)
A point with x and y coordinate values.
Definition: Point.h:50
::v8::Persistent<::v8::FunctionTemplate > & GetGeometryTemplate()
It returns a reference to the persistent template of a Geometry object.
virtual std::unique_ptr< Point > getEndPoint() const =0
It returns the curve end point.
::v8::Persistent<::v8::FunctionTemplate > & GetCurveTemplate()
It returns a reference to the persistent template of a Curve object.