binding/v8/jsi/geometry/Surface.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 Surface.cpp
22 
23  \brief Utility functions to register the Surface class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/Coord2D.h"
28 #include "../../../../geometry/Point.h"
29 #include "../../../../geometry/Surface.h"
30 #include "../../common/Utils.h"
31 #include "Geometry.h"
32 
33 ::v8::Handle<::v8::Value> Surface_GetArea(const ::v8::Arguments& args)
34 {
35  ::v8::HandleScope hs;
36 
37  if(args.Holder().IsEmpty())
38  return ::v8::ThrowException(::v8::String::New("In order to use getArea method you must use object notation: \"area = obj.getArea();\""));
39 
40  te::gm::Surface* g = te::v8::common::Unwrap<te::gm::Surface>(args.Holder());
41 
42  if(g == 0)
43  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getArea method!"));
44 
45  double a = g->getArea();
46 
47  ::v8::Local<::v8::Number> ja = ::v8::Number::New(a);
48 
49  return hs.Close(ja);
50 }
51 
52 ::v8::Handle<::v8::Value> Surface_GetCentroid(const ::v8::Arguments& args)
53 {
54  ::v8::HandleScope hs;
55 
56  if(args.Holder().IsEmpty())
57  return ::v8::ThrowException(::v8::String::New("In order to use getCentroid method you must use object notation: \"pt = obj.getCentroid();\""));
58 
59  te::gm::Surface* g = te::v8::common::Unwrap<te::gm::Surface>(args.Holder());
60 
61  if(g == 0)
62  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getCentroid method!"));
63 
64  std::auto_ptr<te::gm::Point> pt(g->getCentroid());
65 
66  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
67 
68  pt.release();
69 
70  return hs.Close(jsgeom);
71 }
72 
73 ::v8::Handle<::v8::Value> Surface_GetCentroidCoord(const ::v8::Arguments& args)
74 {
75  ::v8::HandleScope hs;
76 
77  if(args.Holder().IsEmpty())
78  return ::v8::ThrowException(::v8::String::New("In order to use getCentroidCoord method you must use object notation: \"c = obj.getCentroidCoord();\""));
79 
80  te::gm::Surface* g = te::v8::common::Unwrap<te::gm::Surface>(args.Holder());
81 
82  if(g == 0)
83  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getCentroidCoord method!"));
84 
85  std::auto_ptr<te::gm::Coord2D> c(g->getCentroidCoord());
86 
87  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(c.get(), te::v8::jsi::GetCoord2DTemplate, true);
88 
89  c.release();
90 
91  return hs.Close(jsgeom);
92 }
93 
94 ::v8::Handle<::v8::Value> Surface_GetPointOnSurface(const ::v8::Arguments& args)
95 {
96  ::v8::HandleScope hs;
97 
98  if(args.Holder().IsEmpty())
99  return ::v8::ThrowException(::v8::String::New("In order to use getPointOnSurface method you must use object notation: \"pt = obj.getPointOnSurface();\""));
100 
101  te::gm::Surface* g = te::v8::common::Unwrap<te::gm::Surface>(args.Holder());
102 
103  if(g == 0)
104  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getPointOnSurface method!"));
105 
106  std::auto_ptr<te::gm::Point> pt(g->getPointOnSurface());
107 
108  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(pt.get(), te::v8::jsi::GetPointTemplate, true);
109 
110  pt.release();
111 
112  return hs.Close(jsgeom);
113 }
114 
115 ::v8::Handle<::v8::Value> Surface_GetCoordOnSurface(const ::v8::Arguments& args)
116 {
117  ::v8::HandleScope hs;
118 
119  if(args.Holder().IsEmpty())
120  return ::v8::ThrowException(::v8::String::New("In order to use getCoordOnSurface method you must use object notation: \"c = obj.getCoordOnSurface();\""));
121 
122  te::gm::Surface* g = te::v8::common::Unwrap<te::gm::Surface>(args.Holder());
123 
124  if(g == 0)
125  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getCoordOnSurface method!"));
126 
127  std::auto_ptr<te::gm::Coord2D> c(g->getCoordOnSurface());
128 
129  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(c.get(), te::v8::jsi::GetCoord2DTemplate, true);
130 
131  c.release();
132 
133  return hs.Close(jsgeom);
134 }
135 
136 static ::v8::Persistent<::v8::FunctionTemplate> sg_surface_template;
137 
138 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetSurfaceTemplate()
139 {
140  if(sg_surface_template.IsEmpty())
141  {
142  ::v8::Persistent<::v8::FunctionTemplate>& geomTpl = GetGeometryTemplate();
143  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
144  result->Inherit(geomTpl);
145 
146  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
147 
148  prototype->Set(::v8::String::NewSymbol("getArea"), ::v8::FunctionTemplate::New(Surface_GetArea));
149  prototype->Set(::v8::String::NewSymbol("getCentroid"), ::v8::FunctionTemplate::New(Surface_GetCentroid));
150  prototype->Set(::v8::String::NewSymbol("getCentroidCoord"), ::v8::FunctionTemplate::New(Surface_GetCentroidCoord));
151  prototype->Set(::v8::String::NewSymbol("getPointOnSurface"), ::v8::FunctionTemplate::New(Surface_GetPointOnSurface));
152  prototype->Set(::v8::String::NewSymbol("getCoordOnSurface"), ::v8::FunctionTemplate::New(Surface_GetCoordOnSurface));
153 
154  sg_surface_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
155  }
156 
157  return sg_surface_template;
158 }
159 
::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.
virtual Point * getCentroid() const =0
It returns the mathematical centroid for the surface as a point.
::v8::Persistent<::v8::FunctionTemplate > & GetCoord2DTemplate()
It returns a reference to the persistent template of a Coord2D object.
Definition: Coord2D.cpp:144
virtual Coord2D * getCentroidCoord() const =0
It returns the mathematical centroid for this surface as a coordinate.
static::v8::Persistent<::v8::FunctionTemplate > sg_surface_template
virtual double getArea() const =0
It returns the area of the surface measured in the spatial reference system of the surface...
::v8::Handle<::v8::Value > Surface_GetCoordOnSurface(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Surface_GetCentroidCoord(const ::v8::Arguments &args)
virtual Point * getPointOnSurface() const =0
It returns a point guaranteed to be on this surface.
::v8::Handle<::v8::Value > Surface_GetCentroid(const ::v8::Arguments &args)
virtual Coord2D * getCoordOnSurface() const =0
It returns a coordinate guaranteed to be on this surface.
::v8::Persistent<::v8::FunctionTemplate > & GetGeometryTemplate()
It returns a reference to the persistent template of a Geometry object.
::v8::Handle<::v8::Value > Surface_GetArea(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetSurfaceTemplate()
It returns a reference to the persistent template of a Surface object.
::v8::Handle<::v8::Value > Surface_GetPointOnSurface(const ::v8::Arguments &args)
Surface is an abstract class that represents a 2-dimensional geometric objects.
Definition: Surface.h:54