<commit_before>/* Copyright (C) 2015, WSID   */

/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define _CRANKSHAPE_INSIDE

#include <glib.h>
#include <glib-object.h>
#include "crankbase.h"
#include "crankshape2.h"
#include "crankshape2finite.h"

#include "crankshape2vertexed.h"

/**
 * SECTION: crankshape2vertexed
 * @title: CrankShape2Vertexed
 * @short_description: Vertex based shapes.
 * @stability: Unstable
 * @include: crankshape.h
 *
 * This represents shapes with vertices, and possibly edges.
 *
 * # Abstract Functions
 *
 * * #CrankShape2VertexedClass.get_dimension()
 * * #CrankShape2VertexedClass.get_nvertices()
 * * #CrankShape2VertexedClass.get_nedges()
 * * #CrankShape2VertexedClass.get_vertex_pos()
 * * #CrankShape2VertexedClass.get_vertex_edges()
 * * #CrankShape2VertexedClass.get_edge_vertices()
 *
 * # Abstract Functions left
 *
 * * #CrankShape2Class.contains()
 * * #CrankShape2FiniteClass.is_convex()
 * * #CrankShape2FiniteClass.get_bound_radius()
 *
 * # Virtual Functions
 *
 * * #CrankShape2FiniteClass.approximate_vertexed(): As this is already
 *   vertex based shape, default implementation will return itself as
 *   approximation.
 *
 * * #CrankShape2VertexedClass.get_farthest_vertex(): Default implementation
 *   will iterates over all vertices and pick the farthest one.
 *
 */
//////// List of virtual functions /////////////////////////////////////////////

static void crank_shape2_vertexed_get_property (GObject    *object,
                                                guint       prop_id,
                                                GValue     *value,
                                                GParamSpec *pspec);


static CrankShape2Vertexed *crank_shape2_vertexed_approximate_vertexed (CrankShape2Finite *shape);

//////// Default Implementation ////////////////////////////////////////////////

static guint crank_shape2_vertexed_def_get_farthest_vertex (CrankShape2Vertexed *shape,
                                                            CrankVecFloat2      *direction);


//////// Properties and Signals ////////////////////////////////////////////////

enum {
  PROP_0,
  PROP_DIMENSION,
  PROP_NVERTICES,
  PROP_NEDGES,
  PROP_COUNTS
};

static GParamSpec *pspecs[PROP_COUNTS] = {NULL};

//////// Type definition ///////////////////////////////////////////////////////

G_DEFINE_ABSTRACT_TYPE (CrankShape2Vertexed,
                        crank_shape2_vertexed,
                        CRANK_TYPE_SHAPE2_FINITE)

//////// GTypeInstance /////////////////////////////////////////////////////////

static void
crank_shape2_vertexed_init (CrankShape2Vertexed *self)
{
}

static void
crank_shape2_vertexed_class_init (CrankShape2VertexedClass *c)
{
  GObjectClass *c_gobject;
  CrankShape2FiniteClass *c_shape2finite;

  c_gobject = G_OBJECT_CLASS (c);
  c_gobject->get_property = crank_shape2_vertexed_get_property;

  pspecs[PROP_DIMENSION] = g_param_spec_uint (
      "dimension", "dimension", "Dimension of vertexed shape.",
      0, 2, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  pspecs[PROP_NVERTICES] = g_param_spec_uint (
      "nvertices", "nvertices", "Number of vertices.",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  pspecs[PROP_NEDGES] = g_param_spec_uint (
      "nedges", "nedges", "Number of edges.",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (c_gobject, PROP_COUNTS, pspecs);


  c_shape2finite = CRANK_SHAPE2_FINITE_CLASS (c);

  c_shape2finite->approximate_vertexed = crank_shape2_vertexed_approximate_vertexed;


  c->get_farthest_vertex = crank_shape2_vertexed_def_get_farthest_vertex;

}

//////// GObject ///////////////////////////////////////////////////////////////

static void
crank_shape2_vertexed_get_property (GObject    *object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  CrankShape2Vertexed *shape = CRANK_SHAPE2_VERTEXED (shape);

  switch (prop_id)
    {
    case PROP_DIMENSION:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_dimension (shape));
      break;

    case PROP_NVERTICES:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_nvertices (shape));
      break;

    case PROP_NEDGES:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_nedges (shape));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

//////// CrankShape2Finite /////////////////////////////////////////////////////

static CrankShape2Vertexed*
crank_shape2_vertexed_approximate_vertexed (CrankShape2Finite *shape)<commit_msg>CrankShape2Vertexed: Implemented get_bound_radius () vfunc.<commit_after>/* Copyright (C) 2015, WSID   */

/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define _CRANKSHAPE_INSIDE

#include <glib.h>
#include <glib-object.h>
#include "crankbase.h"
#include "crankshape2.h"
#include "crankshape2finite.h"

#include "crankshape2vertexed.h"

/**
 * SECTION: crankshape2vertexed
 * @title: CrankShape2Vertexed
 * @short_description: Vertex based shapes.
 * @stability: Unstable
 * @include: crankshape.h
 *
 * This represents shapes with vertices, and possibly edges.
 *
 * # Abstract Functions
 *
 * * #CrankShape2VertexedClass.get_dimension()
 * * #CrankShape2VertexedClass.get_nvertices()
 * * #CrankShape2VertexedClass.get_nedges()
 * * #CrankShape2VertexedClass.get_vertex_pos()
 * * #CrankShape2VertexedClass.get_vertex_edges()
 * * #CrankShape2VertexedClass.get_edge_vertices()
 *
 * # Abstract Functions left
 *
 * * #CrankShape2Class.contains()
 * * #CrankShape2FiniteClass.is_convex()
 * * #CrankShape2FiniteClass.get_bound_radius()
 *
 * # Virtual Functions
 *
 * * #CrankShape2FiniteClass.approximate_vertexed(): As this is already
 *   vertex based shape, default implementation will return itself as
 *   approximation.
 *
 * * #CrankShape2VertexedClass.get_farthest_vertex(): Default implementation
 *   will iterates over all vertices and pick the farthest one.
 *
 */
//////// List of virtual functions /////////////////////////////////////////////

static void crank_shape2_vertexed_get_property (GObject    *object,
                                                guint       prop_id,
                                                GValue     *value,
                                                GParamSpec *pspec);

static gfloat               crank_shape2_vertexed_get_bound_radius (CrankShape2Finite *shape);

static CrankShape2Vertexed *crank_shape2_vertexed_approximate_vertexed (CrankShape2Finite *shape);

//////// Default Implementation ////////////////////////////////////////////////

static guint crank_shape2_vertexed_def_get_farthest_vertex (CrankShape2Vertexed *shape,
                                                            CrankVecFloat2      *direction);


//////// Properties and Signals ////////////////////////////////////////////////

enum {
  PROP_0,
  PROP_DIMENSION,
  PROP_NVERTICES,
  PROP_NEDGES,
  PROP_COUNTS
};

static GParamSpec *pspecs[PROP_COUNTS] = {NULL};

//////// Type definition ///////////////////////////////////////////////////////

G_DEFINE_ABSTRACT_TYPE (CrankShape2Vertexed,
                        crank_shape2_vertexed,
                        CRANK_TYPE_SHAPE2_FINITE)

//////// GTypeInstance /////////////////////////////////////////////////////////

static void
crank_shape2_vertexed_init (CrankShape2Vertexed *self)
{
}

static void
crank_shape2_vertexed_class_init (CrankShape2VertexedClass *c)
{
  GObjectClass *c_gobject;
  CrankShape2FiniteClass *c_shape2finite;

  c_gobject = G_OBJECT_CLASS (c);
  c_gobject->get_property = crank_shape2_vertexed_get_property;

  pspecs[PROP_DIMENSION] = g_param_spec_uint (
      "dimension", "dimension", "Dimension of vertexed shape.",
      0, 2, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  pspecs[PROP_NVERTICES] = g_param_spec_uint (
      "nvertices", "nvertices", "Number of vertices.",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  pspecs[PROP_NEDGES] = g_param_spec_uint (
      "nedges", "nedges", "Number of edges.",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (c_gobject, PROP_COUNTS, pspecs);


  c_shape2finite = CRANK_SHAPE2_FINITE_CLASS (c);

  c_shape2finite->get_bound_radius = crank_shape2_vertexed_get_bound_radius;
  c_shape2finite->approximate_vertexed = crank_shape2_vertexed_approximate_vertexed;


  c->get_farthest_vertex = crank_shape2_vertexed_def_get_farthest_vertex;

}

//////// GObject ///////////////////////////////////////////////////////////////

static void
crank_shape2_vertexed_get_property (GObject    *object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  CrankShape2Vertexed *shape = CRANK_SHAPE2_VERTEXED (shape);

  switch (prop_id)
    {
    case PROP_DIMENSION:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_dimension (shape));
      break;

    case PROP_NVERTICES:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_nvertices (shape));
      break;

    case PROP_NEDGES:
      g_value_set_uint (value,
                        crank_shape2_vertexed_get_nedges (shape));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

//////// CrankShape2Finite /////////////////////////////////////////////////////

static gfloat
crank_shape2_vertexed_get_bound_radius (CrankShape2Finite *shape)
{
  CrankShape2Vertexed *self = (CrankShape2Vertexed*)shape;

  CrankVecFloat2 vpos;
  gfloat magn_sq;

  guint i, n;
  n = crank_shape2_vertexed_get_nvertices (self);

  if (n == 0) return 0;

  crank_shape2_vertexed_get_vertex_pos (self, 0, &vpos);
  magn_sq = crank_vec_float2_get_magn_sq (&vpos);

  for (i = 1; i < n; i++)
    {
      gfloat magn_sq_c;

      crank_shape2_vertexed_get_vertex_pos (self, i, &vpos);
      magn_sq_c = crank_vec_float2_get_magn_sq (&vpos);

      magn_sq = MAX (magn_sq, magn_sq_c);
    }

  return sqrtf (magn_sq);
}


static CrankShape2Vertexed*
crank_shape2_vertexed_approximate_vertexed (CrankShape2Finite *shape)
{'fraction_non_alphanumeric': 0.13690190430468874, 'fraction_numerical': 0.011121779484717554, 'mean_word_length': 2.5218371372217527, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7036362', 'n_tokens_mistral': 3864, 'n_tokens_neox': 3689, 'n_words': 883}