summaryrefslogtreecommitdiff
path: root/axon_direction_model.cc
blob: c5188c4b8afcbbed46adfbe6a39cf616e2e96040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
  © Copyright 2008 Randal A. Koene <randalk@netmorph.org>
  
  With design assistance from J. van Pelt & A. van Ooyen, and support
  from the Netherlands Organization for Scientific Research (NWO)
  Program Computational Life Sciences grant CLS2003 (635.100.005) and
  from the EC Marie Curie Research and Training Network (RTN)
  NEURoVERS-it 019247.

  This file is part of NETMORPH.

  NETMORPH is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  NETMORPH is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with NETMORPH.  If not, see <http://www.gnu.org/licenses/>.
*/
// axon_direction_model.cc
// Randal A. Koene, 20051021

/* Documentation:
   This implements base and derived classes for models of axon direction during
   network development that are based on curvature statistics (rather than
   causal hypotheses about environmental interactions).
   The base model provides a reference to the history of an axon's growth.
 */

#include <math.h>
#include "axon_direction_model.hh"
#include "neuron.hh"
#include "fibre_structure.hh"
#include "dendritic_growth_model.hh"
#include "environment_physics.hh"
#include "Spatial_Presentation.hh"

/* [***INCOMPLETE] Here I can include functions that parse the command line for
   general parameters to be used in specific direction models if no neuron or
   terminal segment specific parameters are given.
 */

// global variables
// See http://rak.minduploading.org:8080/caspan/Members/randalk/model-specification-implementation/.
direction_model default_direction_model = radial_dm; // identifier, changed when a different universal model is set
String universal_direction_model_root; // Used only to report if chaining at the universal set level.

general_direction_model_parameters_interface general_direction_model_parameters;

// Region and natural subset specific schemas (initialized in neuron.cc:general_neuron_parameters_interface::parse_CLP(Command_Line_Parameters & clp, network & net))
region_direction_model_base_ptr * direction_model_region_subset_schemas = NULL;


void general_direction_model_parameters_interface::parse_CLP(Command_Line_Parameters & clp) {
  int n;
  if ((n=clp.Specifies_Parameter("dirhistory_selection"))>=0) {
    if (downcase(clp.ParValue(n))==String("random_truncation")) direction_history_selection_model = random_truncation_dhs;
    else direction_history_selection_model = no_dhs;
  }
  if ((n=clp.Specifies_Parameter("general_rtdhs_probability"))>=0) random_truncation_dh_probability = atof(clp.ParValue(n));
  if ((n=clp.Specifies_Parameter("general_rtdhs_minfraction"))>=0) {
    random_truncation_dh_minfraction = atof(clp.ParValue(n));
    if (random_truncation_dh_minfraction<0.0) random_truncation_dh_minfraction = 0.0;
    if (random_truncation_dh_minfraction>1.0) random_truncation_dh_minfraction = 1.0;
  }
  if ((n=clp.Specifies_Parameter("general_rtdhs_maxfraction"))>=0) {
    random_truncation_dh_maxfraction = atof(clp.ParValue(n));
    if (random_truncation_dh_maxfraction<random_truncation_dh_minfraction) random_truncation_dh_maxfraction = random_truncation_dh_minfraction;
    if (random_truncation_dh_maxfraction>1.0) random_truncation_dh_maxfraction = 1.0;
  }
}

String general_direction_model_parameters_interface::report_parameters() {
  String res("Fiber growth direction hypotheses:\n");
  switch (direction_history_selection_model) {
  case random_truncation_dhs:
    res += "  random fiber segment history truncation.\n    probability = ";
    res += String(random_truncation_dh_probability,"%.3f, truncated to a fraction between ");
    res += String(random_truncation_dh_minfraction,"%.3f and ");
    res += String(random_truncation_dh_maxfraction,"%.3f.\n");
    break;;
  default: res += "  fiber segment history is not modified.\n";
  }
  return res;
}

axon_direction_history::axon_direction_history(): dhs(NULL), effective_length(0.0) {
  // [***INCOMPLETE] This currently uses only the general default, even though
  // axon_direction_history objects are allocated one per terminal segment, so
  // that even terminal segment specific choices are possible.
  switch (general_direction_model_parameters.direction_history_selection_model) {
  case random_truncation_dhs: dhs = new random_truncation_dh_selection(); break;;
  default: break;; // to avoid a compilation warning message about no_dhs
  }
}

axon_direction_element::axon_direction_element(fibre_segment * _fs) {
  u = _fs->P1;
  u -= _fs->P0;
  l = _fs->Length();
  if (l!=0.0) u /= l;
  //r = l/2.0; // [***INCOMPLETE] May depend on integration method and r dependence function.
}

void pll_axon_direction_history::clear() {
  axon_direction_history::clear();
  adr.clear();
  if (dhs) dhs->history_reset();
}

void pll_axon_direction_history::update_effective_history() {
  if (dhs) dhs->effective_history(this);
}

fibre_segment * parse_axon_direction_history::seek(int idx) {
  fibre_segment * fs = firstafterbranch;
  for (; idx>0; idx--) {
    if (fs->Branch1()) fs = fs->Branch1();
    else if (fs->Branch2()) fs = fs->Branch2();
    else return NULL;
  }
  return fs;
}

void parse_axon_direction_history::clear(fibre_segment & fab) {
  axon_direction_history::clear();
  firstafterbranch = &fab;
  skipafterbranch = 0;
  segmentsafterbranch = 0;
  if (dhs) dhs->history_reset();
}

void parse_axon_direction_history::next_further() {
  if (segidx>=0) remaining -= segidxfs->Length();
  if ((remaining>0.0) && (segidx>skipafterbranch)) {
    segidx--;
    segidxfs = seek(segidx);
  } else segidx = -1;
}

double parse_axon_direction_history::segment_length() {
  if (segidx<0) return -1.0;
  double l = segidxfs->Length();
  if (l<remaining) return l;
  return remaining;
}

spatial & parse_axon_direction_history::segment_unit_direction() {
  if (segidx<0) u.set_all(0.0,0.0);
  else {
    u = segidxfs->P1;
    u -= segidxfs->P0;
    u /= segidxfs->Length();
  }
  return u;
}

void parse_axon_direction_history::update_effective_history() {
  if (dhs) dhs->effective_history(this);
}

void random_truncation_dh_selection::effective_history(axon_direction_history * adh) {
  // [***INCOMPLETE] This uses the general parameter values, not any specific
  // parameter values set for this history selection object.
  if (X_turn.get_rand_real1()<general_direction_model_parameters.random_truncation_dh_probability) {
    adh->effective_length *= X_turn.get_rand_range_real1(general_direction_model_parameters.random_truncation_dh_minfraction,general_direction_model_parameters.random_truncation_dh_maxfraction);
  }
}

direction_model_base::direction_model_base(direction_model_base * dmcontrib, double & dmweight, direction_model_base & schema):
#ifdef ADM_PRIORITIZE_SPEED_OVER_SPACE
  admpdf(schema.admpdf),
#endif
  contributing(NULL), contributingweight(1.0), base_parameters(schema.base_parameters) {
  // [***INCOMPLETE] More specific choices for parameters, e.g. the veer
  // limits of the probability distribution, can be detected here. Those may
  // be specified for the network, the type of neuron, the neuron, axons or
  // dendrites, even for a specific arbor or growth cone (terminal segment).
  // The default values are set to match global parameters defined for this
  // particular or a related model (e.g. veeranglemin/max=turnanglemin/max).
  // Can I suggest inlining this constructor somehow (without moving it and
  // the include files that make turnanglemin/max available to the .hh file?
  if (dmcontrib) { // Clone chained models
    contributingweight = dmweight;
    contributing = dmcontrib->clone();
  }
}

direction_model_base::direction_model_base(String & thislabel, String & label, Command_Line_Parameters & clp):
  admpdf(X_turn),
  contributing(NULL), contributingweight(1.0), base_parameters(turnanglemin,turnanglemax) {
  // [***INCOMPLETE] More specific choices for parameters, e.g. the veer
  // limits of the probability distribution, can be detected here. Those may
  // be specified for the network, the type of neuron, the neuron, axons or
  // dendrites, even for a specific arbor or growth cone (terminal segment).
  // The default values are set to match global parameters defined for this
  // particular or a related model (e.g. veeranglemin/max=turnanglemin/max).
  // Can I suggest inlining this constructor somehow (without moving it and
  // the include files that make turnanglemin/max available to the .hh file?
  // [***NOTE] This is not a CLP_Modifiable, because I do not wish to have to
  // store the label in order to be able to parse_CLP at any time.
  int n;
  double vmin = base_parameters.veeranglemin;
  double vmax = base_parameters.veeranglemax;
  if ((n=clp.Specifies_Parameter(thislabel+"veeranglemin"))>=0) {
    vmin = atof(clp.ParValue(n));
    if (vmin<0.0) {
      warning("Warning: "+thislabel+"veeranglemin="+clp.ParValue(n)+" not permitted, value must be >=0.0, not modified\n");
      vmin = base_parameters.veeranglemin;
    }
  }
  if ((n=clp.Specifies_Parameter(thislabel+"veeranglemax"))>=0) {
    vmax = atof(clp.ParValue(n));
    if (vmax<=0.0) {
      warning("Warning: "+thislabel+"veeranglemax="+clp.ParValue(n)+" not permitted, value must be >0.0, not modified\n");
      vmax = base_parameters.veeranglemax;
    }
  }
  if (vmax<=vmin) warning("Warning: "+thislabel+"veeranglemax must be > "+thislabel+"veeranglemin, not modified\n");
  else {
    base_parameters.veeranglemin = vmin;
    base_parameters.veeranglemax = vmax;
  }
#ifdef ADM_PRIORITIZE_SPEED_OVER_SPACE
  admpdf.setup(base_parameters.veeranglemax-base_parameters.veeranglemin);
#endif
  if (label.empty()) return;
  if ((n=clp.Specifies_Parameter(label+"dm_weight"))>=0) contributingweight = atof(clp.ParValue(n));
  if (!direction_model_selection(label,clp,contributing,contributing)) error("Error in direction_model_base(): No contributing direction model found for "+label+".\n");
}

String direction_model_base::report_parameters() {
  // This calls report_parameters_specific() in any derived class and
  // propagates parameter reporting tocontributing models.
  String res(report_parameters_specific());
  res += String(base_parameters.veeranglemin,"\n  [veeranglemin=%.2f] ");
  res += String(base_parameters.veeranglemax,"\n  [veeranglemax=%.2f] ");
  if (contributing) {
    res += String(contributingweight,"\n  [contrib.w=%.2f] ");
    res += contributing->report_parameters();
  }
  return res;
}

void direction_model_base::turn(terminal_segment * ts, spatial & predicted, double initlen) {
  // Using the resulting preferred direction found in direction(), apply
  // random deviation.
  // ts points to the terminal segment before the turn
  // [***INCOMPLETE] Create a class as in DIL#20051129041848.1 and use it here.
#ifndef ADM_PRIORITIZE_SPEED_OVER_SPACE
  linear_pdf admpdf(base_parameters.veeranglemax - base_parameters.veeranglemin); // [***INCOMPLETE] The pdf is not yet an option.
#endif
#ifdef VECTOR3D
  // See TL#200511261312.2.
  //  double y = max_y*random_double();
  // double veerangle = (negadm_b + sqrt(SQadm_b + (twoa*y)))/a;
  double veerangle = admpdf.random_positive() + base_parameters.veeranglemin;
  spatial S1xyz;
  double rollangle = 2.0*M_PI*X_turn.get_rand_real1();
  spatial S1(1.0,rollangle,veerangle);
  prepare_parent_coordinate_system(predicted);
  daughter_to_network_coordinate_system(S1,S1xyz);
#endif
#ifdef VECTOR2D
  // See TL#200511261312.2.
  //double r = twomax_y*random_double();
  //double y = r;
  //if (r>max_y) y -= max_y;
  //double veerangle = (negadm_b + sqrt(SQadm_b + (twoa*y)))/a;
  double veerangle = admpdf.random_selection();
  if (veerangle>=0.0) veerangle += base_parameters.veeranglemin;
  else veerangle -= base_parameters.veeranglemin;
  spatial S1xyz(0.0,predicted.Y()+veerangle);
#endif
  // The following are the morphological updates applied:
  // 1. specify a turn that also results in a new terminal segment object
  S1xyz.set_X(initlen);
#ifdef VECTOR3D
  extra_fibre_data efd(rollangle);
  ts->turn_without_update_of_segment_vector(S1xyz,efd);
#endif
#ifdef VECTOR2D
  ts->turn_without_update_of_segment_vector(S1xyz);
#endif
  // 2. update turn (curvature) statistics
#ifdef SAMPLES_INCLUDE_NETWORK_STATISTICS_BASE
#ifdef INCLUDE_TURN_ANGLE_SAMPLING
  const stat_labels angledatalabel[2] = {axons_turn_angles,dendrites_turn_angles};
  if (sampled_output->netstats[angledatalabel[growing_dendrites]].Collect()) {
    STANDARDIZE_ANGLE_DATA(randomangle);
    // [***INCOMPLETE] The veerangle here is not the actual turn angle!
    sampled_output->netstats[angledatalabel[growing_dendrites]].Collect_Data(veerangle);
  }
#endif
#endif
}

tension_direction_model::tension_direction_model(axon_direction_history & _adh, direction_model_base * dmbcontrib, double & dmbweight,  tension_direction_model & schema): direction_model_base(dmbcontrib,dmbweight,schema), adh(&_adh), tension_parameters(schema.tension_parameters) {
}

tension_direction_model::tension_direction_model(axon_direction_history & _adh, String & thislabel, String & label, Command_Line_Parameters & clp): direction_model_base(thislabel,label,clp), adh(&_adh), tension_parameters(2.0) {
  int n;
  if ((n=clp.Specifies_Parameter(thislabel+"history_power"))>=0) tension_parameters.history_power =  atof(clp.ParValue(n));
  // Chaining of models is taken care of in the base constructor.
}

String tension_direction_model::report_parameters_specific() {
  String res(" segment history tension (history_power = ");
  res += String(tension_parameters.history_power,"%.3f)");
  return res;
}

direction_model_base * tension_direction_model::clone() {
#ifdef ADM_PRIORITIZE_SPEED_OVER_SPACE
  return new tension_direction_model(*(new pll_axon_direction_history()),contributing,contributingweight,*this);
#else
  // [***INCOMPLETE] Clearly, this does not work as implemented here,
  // because ts2 is not available. Consider if I should keep the space
  // conserving version at all.
  return new tension_direction_model(*(new parse_axon_direction_history(ts2.TerminalSegment())),contributing,contributingweight,*this)
#endif
}

void tension_direction_model::handle_branching(terminal_segment & ts1, terminal_segment & ts2) {
  // After branching the terminal segments on each branch need a
  // history. The first branch receives this history object after
  // clearing. The second branch receives a history object of
  // the same type.
  // [***INCOMPLETE] Using a #define to select the history class
  // defeats the purpose of compiling the code for multiple.
  ts2.set_direction_model(clone());
#ifdef ADM_PRIORITIZE_SPEED_OVER_SPACE
  adh->clear();
#else
  adh->clear(ts1.TerminalSegment());
#endif
  ts1.set_direction_model(this);
}

void tension_direction_model::handle_turning(terminal_segment & ts) {
  // ts is the new terminal_segment.
  // Here, any computations that must be made on data, e.g. for the maintenance
  // of valid values in the history can be done.
  adh->update_effective_history();
  ts.set_direction_model(this);
}

void tension_direction_model::predict_direction(spatial & predicted) {
  // Calculate historical segment effects, multiply with unit direction
  // vectors and sum.
  // The historical segment effect is modeled in a manner analogous to
  // gravitation in this class.
  // It is expected that predicted is initialized to zero, unless the
  // following calculations should add to it.
  adh->restart_at_nearest();
  double distancebyfibre = 0.0, L; 
  for (; (L=adh->segment_length())>=0.0; adh->next_further()) if (L>0.0) {
    double mass = L * M_PI * adh->segment_radius_squared();
    double distance = distancebyfibre + (L/2.0);
    double relativegravitation = mass / pow(distance,tension_parameters.history_power);
    predicted += relativegravitation*adh->segment_unit_direction();
    distancebyfibre += L;
  }
}

spatial tension_direction_model::predict(double weight, terminal_segment * ts, neuron * e) {
  // Called in a chain of direction models to apply, where the first model
  // in the chain is automatically assigned the weight 1.0 and the weighting
  // of other direction models is proportional to that (i.e. it can be > 1.0).
  spatial predicted;
  predict_direction(predicted);
  predicted.normalize();
  predicted *= weight;
  if (contributing) predicted += contributing->predict(contributingweight,ts,e);
  return predicted;
}

void tension_direction_model::direction(terminal_segment * ts, neuron * e, double initlen) {
  // ts points to the terminal segment before the turn
  // e points to the neuron to which the arbor belongs
  // The following are the preparations of this axon direction model:
  // 1. update the spatial parameters of the terminal fibre segment
  ts->update_segment_vector(); // hence, use ts->turn_free()
  // 2. include the terminal fibre segment in the history
  adh->add(ts->TerminalSegment(),ts->Length());
  // The following are the functions of this axon direction model:
  // 1. determine "pull" strengths of the preceding segments
  // NOTE: This version uses a gravitation analogy for the calculation of the
  // amplitude of the effect that each segment has on the predicted direction.
  // See TL#200511180719.1 and TL#200511181155.1.
  // 2. using pull strength as length sum direction vectors
  spatial predicted;
  predict_direction(predicted);
  if (contributing) {
    predicted.normalize(); // only necessary here
    predicted += contributing->predict(contributingweight,ts,e);
  }
  predicted.convert_to_spherical();
  if (predicted.X()==0.0) { // [***INCOMPLETE] See TL.
#ifdef DEBUGGING_DIRECTION
    debugging.tension_direction_model_no_direction++;
#endif
    predicted = ts->AngularCoords();
  }
  // [*** NOTE:] The probability distribution of the veer angle is currently
  // not dependent on the history size, which currently has an effect only
  // on the predicted direction (i.e. mean).
  // 3. make the turn with probabilistic veering around the predicted direction
  turn(ts,predicted,initlen);
}

radial_direction_model::radial_direction_model(direction_model_base * dmbcontrib, double & dmbweight,  radial_direction_model & schema): direction_model_base(dmbcontrib,dmbweight,schema), radial_parameters(schema.radial_parameters) {
}

radial_direction_model::radial_direction_model(String & thislabel, String & label, Command_Line_Parameters & clp): direction_model_base(thislabel,label,clp), radial_parameters(false) {
  int n;
  if ((n=clp.Specifies_Parameter(thislabel+"Samsonovich_hypothesis"))>=0) radial_parameters.Samsonovich_hypothesis = (downcase(clp.ParValue(n))==String("true"));
  // Chaining of models is taken care of in the base constructor.
}

String radial_direction_model::report_parameters_specific() {
  String res(" radial");
  if (radial_parameters.Samsonovich_hypothesis) res += " (with Samsonovich hypothesis)";
  else res += " (no Samsonovich hypothesis)";
  return res;
}

direction_model_base * radial_direction_model::clone() {
  return new radial_direction_model(contributing,contributingweight,*this);
}

void radial_direction_model::handle_branching(terminal_segment & ts1, terminal_segment & ts2) {
  ts2.set_direction_model(clone());
  ts1.set_direction_model(this);
}

void radial_direction_model::handle_turning(terminal_segment & ts) {
  // ts is the new terminal_segment.
  ts.set_direction_model(this);
}

void radial_direction_model::predict_direction(spatial & predicted, neuron * n, terminal_segment * ts) {
  // This model function predicts a direction that is the same as the parent
  // direction, or a radial direction if the Samsonovich hypothesis is applied.
  // (This model is the replacement for the legacy direction code used in older
  // versions of NETMORPH, see TL#200808041538.1.)
  if (radial_parameters.Samsonovich_hypothesis) {
    fibre_segment * fs = ts->TerminalSegment();
    predicted = fs->P1;
    predicted -= fs->N()->Pos();
  } else {
    predicted = ts->AngularCoords();
    predicted.convert_from_spherical(); // to make summation of normalized contributions and then conversion back to spherical possible in direction()
  }
}

spatial radial_direction_model::predict(double weight, terminal_segment * ts, neuron * e) {
  // Called in a chain of direction models to apply, where the first model
  // in the chain is automatically assigned the weight 1.0 and the weighting
  // of other direction models is proportional to that (i.e. it can be > 1.0).
  spatial predicted;
  predict_direction(predicted,e,ts);
  predicted.normalize();
  predicted *= weight;
  if (contributing) predicted += contributing->predict(contributingweight,ts,e);
  return predicted;
}

void radial_direction_model::direction(terminal_segment * ts, neuron * e, double initlen) {
  // ts points to the terminal segment before the turn
  // e points to the neuron to which the arbor belongs
  ts->update_segment_vector(); // hence, use ts->turn_free()
  spatial predicted;
  predict_direction(predicted,e,ts);
  if (contributing) {
    predicted.normalize(); // only necessary here
    predicted += contributing->predict(contributingweight,ts,e);
  }
  predicted.convert_to_spherical();
  if (predicted.X()==0.0) { // [***INCOMPLETE] See TL.
    predicted = ts->AngularCoords();
  }
  // make the turn with probabilistic veering around the predicted direction
  turn(ts,predicted,initlen);
}

vector_direction_model::vector_direction_model(direction_model_base * dmbcontrib, double & dmbweight,  vector_direction_model & schema): direction_model_base(dmbcontrib,dmbweight,schema), vector_parameters(schema.vector_parameters) {
}

vector_direction_model::vector_direction_model(String & thislabel, String & label, Command_Line_Parameters & clp): direction_model_base(thislabel,label,clp), vector_parameters() {
#ifdef VECTOR3D
  get_spatial(clp,thislabel+"direction",vector_parameters.direction);
#endif
#ifdef VECTOR2D
  warning("Warning: In 2D, the expected direction for the vector direction model is always (0,0).\n");
#endif
  // Chaining of models is taken care of in the base constructor.
}

String vector_direction_model::report_parameters_specific() {
  String res(" vector");
  double x,y,z;
  vector_parameters.direction.get_all(x,y,z);
  res += " (direction = [" + String(x,"%.3f,")+String(y,"%.3f,")+String(z,"%.3f])");
  return res;
}

direction_model_base * vector_direction_model::clone() {
  return new vector_direction_model(contributing,contributingweight,*this);
}

void vector_direction_model::handle_branching(terminal_segment & ts1, terminal_segment & ts2) {
  ts2.set_direction_model(clone());
  ts1.set_direction_model(this);
}

void vector_direction_model::handle_turning(terminal_segment & ts) {
  // ts is the new terminal_segment.
  ts.set_direction_model(this);
}

void vector_direction_model::predict_direction(spatial & predicted, neuron * n, terminal_segment * ts) {
  // This model function predicts a direction along a specified vector.
  predicted = vector_parameters.direction;
}

spatial vector_direction_model::predict(double weight, terminal_segment * ts, neuron * e) {
  // Called in a chain of direction models to apply, where the first model
  // in the chain is automatically assigned the weight 1.0 and the weighting
  // of other direction models is proportional to that (i.e. it can be > 1.0).
  spatial predicted;
  predict_direction(predicted,e,ts);
  predicted.normalize();
  predicted *= weight;
  if (contributing) predicted += contributing->predict(contributingweight,ts,e);
  return predicted;
}

void vector_direction_model::direction(terminal_segment * ts, neuron * e, double initlen) {
  // ts points to the terminal segment before the turn
  // e points to the neuron to which the arbor belongs
  ts->update_segment_vector(); // hence, use ts->turn_free()
  spatial predicted;
  predict_direction(predicted,e,ts);
  if (contributing) {
    predicted.normalize(); // only necessary here
    predicted += contributing->predict(contributingweight,ts,e);
  }
  predicted.convert_to_spherical();
  if (predicted.X()==0.0) { // [***INCOMPLETE] See TL.
    predicted = ts->AngularCoords();
  }
  // make the turn with probabilistic veering around the predicted direction
  turn(ts,predicted,initlen);
}

cell_attraction_direction_model::cell_attraction_direction_model(direction_model_base * dmbcontrib, double & dmbweight, cell_attraction_direction_model & schema): direction_model_base(dmbcontrib,dmbweight,schema) {
}

String cell_attraction_direction_model::report_parameters_specific() {
  String res(" cell attraction");
  return res;
}

direction_model_base * cell_attraction_direction_model::clone() {
  return new cell_attraction_direction_model(contributing,contributingweight,*this);
}

void cell_attraction_direction_model::handle_branching(terminal_segment & ts1, terminal_segment & ts2) {
  ts2.set_direction_model(clone());
  ts1.set_direction_model(this);
}

void cell_attraction_direction_model::handle_turning(terminal_segment & ts) {
  ts.set_direction_model(this);
}

void cell_attraction_direction_model::predict_direction(spatial & predicted, neuron * n, spatial & growthcone) {
#ifdef TESTING_SIMPLE_ATTRACTION
  PLL_LOOP_FORWARD(neuron,n->Root()->head(),1) if (e!=n) if (n->attractedto==e->attracts) {
    spatial d(e->Pos());
    d -= growthcone;
    double SQdistance = d.len2();
    d /= SQdistance; // square distance gravitational analogy
    d /= sqrt(SQdistance); // working with weighted unit vectors
    predicted += d;
  }
#endif
}

spatial cell_attraction_direction_model::predict(double weight, terminal_segment * ts, neuron * e) {
  // Called in a chain of direction models to apply, where the first model
  // in the chain is automatically assigned the weight 1.0 and the weighting
  // of other direction models is proportional to that (i.e. it can be > 1.0).
  spatial predicted;
  predict_direction(predicted,e,ts->TerminalSegment()->P1);
  predicted.normalize();
  predicted *= weight;
  if (contributing) predicted += contributing->predict(contributingweight,ts,e);
  return predicted;
}

void cell_attraction_direction_model::direction(terminal_segment * ts, neuron * e, double initlen) {
  // ts points to the terminal segment before the turn
  // e points to the neuron to which the arbor belongs
  // The following are the preparations of this direction model:
  // 1. update the spatial parameters of the terminal fibre segment
  ts->update_segment_vector(); // hence, use ts->turn_free()
  // The following are the functions of this direction model:
  // 1. predict the direction based on the sum of (chemical) attractions
  //    in a gravitational analogy
  spatial predicted;
  predict_direction(predicted,e,ts->TerminalSegment()->P1);
  if (contributing) {
    predicted.normalize(); // only necessary here
    predicted += contributing->predict(contributingweight,ts,e);
  }
  predicted.convert_to_spherical();
  if (predicted.X()==0.0) {
    warning("Warning: NO DIRECTION PREDICTED in cell_attraction_direction_model::direction()\n"); // [***INCOMPLETE] See TL.
    predicted = ts->AngularCoords();
  }
  // 3. make the turn with probabilistic veering around the predicted direction
  turn(ts,predicted,initlen);
}

String direction_model_idstr[NUM_dm] = {
  "radial",
  "segment_history_tension",
  "cell_attraction",
  "vector"
};

direction_model_base * direction_model_selection(String & label, Command_Line_Parameters & clp, direction_model_base * superior_set, direction_model_base_ptr & dmbptr) {
  // Note: Providing the result pointer in dmbptr enables this function to
  // delete a previously defined schema before linking to a new one.
  // PROTOCOL: [See 200709130802.] Sets for which a direction model is not defined explicitly inherit the direction model of
  //           their specific direct superior by cloning it, thereby also inheriting parameters specified therein.
  direction_model dm = NUM_dm;
  String nextlabel;
  int n;
  // Determine if a specific model is selected for the set with the given label
  if ((n=clp.Specifies_Parameter(label+"direction_model"))>=0) {
    String dmstr(downcase(clp.ParValue(n)));
    for (direction_model i = direction_model(0); i < NUM_dm; i = direction_model(i+1)) if (dmstr==direction_model_idstr[i]) { dm = i; break; }
    if (dm==NUM_dm) {
      if (dmstr==String("legacy")) dm = radial_dm;
      else return NULL; // catch branch_angle_model syntax errors
    }
    if (label.empty()) default_direction_model = dm; // modify default if setting universal
    if (dmstr==String("segment_history_tension")) dm = segment_history_tension_dm;
  } else if (label.empty()) dm = default_direction_model; // the universal set must have a model, since it cannot inherit
  // Determine if a following model selection should include chaining detection
  if ((n=clp.Specifies_Parameter(label+"direction_model_label"))>=0) {
    nextlabel = label+clp.URI_unescape_ParValue(n)+'.';
    if (label.empty()) universal_direction_model_root = nextlabel; // to report at universal level
  } else if ((n=clp.Specifies_Parameter(label+"dm_label"))>=0) { // alternate syntax
    nextlabel = label+clp.URI_unescape_ParValue(n)+'.';
    if (label.empty()) universal_direction_model_root = nextlabel; // to report at universal level
  }
  // Clean up any previously defined schema
  if (dmbptr) dmbptr->delete_shared();
  // Set and return specified model schema or universal model schema if at universal set level
  axon_direction_history * adh = NULL;
  switch (dm) {
  case segment_history_tension_dm: 
#ifdef ADM_PRIORITIZE_SPEED_OVER_SPACE
    adh = new pll_axon_direction_history();
#else
    adh = new parse_axon_direction_history(*this);
#endif
    dmbptr = new tension_direction_model(*adh,label,nextlabel,clp);
    break;;
  case cell_attraction_dm:
    dmbptr = new cell_attraction_direction_model(label,nextlabel,clp);
    break;;
  case radial_dm:
    dmbptr = new radial_direction_model(label,nextlabel,clp);
    break;;
  case vector_dm:
    dmbptr = new vector_direction_model(label,nextlabel,clp);
    break;;
  default: // If not specified, inherit from immediate superior set
    if (!superior_set) return NULL;
    dmbptr = superior_set->clone();
    break;;
  }
  // In case a secondary schema reference is provided through dmbptr for the universal set level, create a clone for the universal set
  if ((label.empty()) && (direction_model_region_subset_schemas[0][universal_sps]!=dmbptr)) {
    if (direction_model_region_subset_schemas[0][universal_sps]) direction_model_region_subset_schemas[0][universal_sps]->delete_shared();
    direction_model_region_subset_schemas[0][universal_sps] = dmbptr->clone();
  }
  return dmbptr;
}