+/*---------- displacement of vertices across a midpoint ----------*/
+
+ /*
+ * Subroutine used where we have
+ *
+ * R - - - - - - - M . - - - - R'
+ * ` .
+ * ` .
+ * S
+ *
+ * and wish to say that the vector RM should be similar to MS
+ * or to put it another way S = M + RM
+ *
+ * NB this is not symmetric wrt R and S since it divides by
+ * |SM| but not |RM| so you probably want to call it twice.
+ *
+ * Details:
+ *
+ * Let R' = M + SM
+ * D = R' - R
+ *
+ * Then the (1/delta)th power of the cost is
+ * proportional to |D|, and
+ * inversely proportional to |SM|
+ * except that |D| is measured in a wierd way which counts
+ * distance in the same direction as SM 1/lambda times as much
+ * ie the equipotential surfaces are ellipsoids around R',
+ * lengthened by lambda in the direction of RM.
+ *
+ * So
+ * delta
+ * [ -1 ]
+ * cost = [ lambda . ( D . SM/|SM| ) + | D x SM/|SM| | ]
+ * R,S,M [ ------------------------------------------- ]
+ * [ |SM| ]
+ *
+ */
+
+static double vertex_one_displ_cost(const double r[D3], const double s[D3],
+ const double midpoint[D3],
+ double delta, double inv_lambda) {
+ const double smlen2_epsilon= 1e-12;
+ double sm[D3], d[D3], ddot, dcross[D3];
+ int k;
+
+ K sm[k]= -s[k] + midpoint[k];
+ K d[k]= midpoint[k] + sm[k] - r[k];
+ ddot= dotprod(d,sm);
+ xprod(dcross, d,sm);
+ double smlen2= magnD2(sm);
+ double cost_basis= inv_lambda * ddot + magnD(dcross);
+ double cost= pow(cost_basis / (smlen2 + smlen2_epsilon), delta);
+
+ return cost;
+}
+
+/*---------- displacement of vertices opposite at a vertex ----------*/
+
+ /*
+ * At vertex Q considering edge direction e to R
+ * and corresponding opposite edge to S.
+ *
+ * This is vertex displacement as above with M=Q
+ */
+
+double vertex_displacement_cost(const Vertices vertices, int section) {
+ const double inv_lambda= 1.0/1; //2;
+ const double delta= 6;
+
+ int si,e,qi,ri;
+ double total_cost= 0;
+
+ FOR_EDGE(qi,e,ri, OUTER) {
+ si= EDGE_END2(qi,(e+3)%V6); if (si<0) continue;
+
+ total_cost += vertex_one_displ_cost(vertices[ri], vertices[si], vertices[qi],
+ delta, inv_lambda);
+ }
+ return total_cost;
+}
+
+/*---------- displacement of vertices opposite at an edge ----------*/
+
+ /*
+ * At edge PQ considering vertices R and S (see diagram
+ * below for overly sharp edge cost).
+ *
+ * Let M = midpoint of PQ
+ */
+
+double vertex_edgewise_displ_cost(const Vertices vertices, int section) {
+ const double inv_lambda= 1.0/1; //2;
+ const double delta= 6;
+
+ int pi,e,qi,ri,si, k;
+ double m[D3];
+ double total_cost= 0;
+
+ FOR_EDGE(pi,e,qi, OUTER) {
+ si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
+ ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
+
+ K m[k]= 0.5 * (vertices[pi][k] + vertices[qi][k]);
+
+ total_cost += vertex_one_displ_cost(vertices[ri], vertices[si], m,
+ delta, inv_lambda);
+ }
+ return total_cost;
+}
+
+
+/*---------- at-vertex edge angles ----------*/