ROOT logo
// $Id: TabletStroke.cxx 2501 2011-07-11 01:39:51Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#include "TabletStroke.h"
#include "TabletStroke.c7"

#include "Gled/XTReqCanvas.h"

#include "TCanvas.h"
#include "TH2I.h"

// TabletStroke

//______________________________________________________________________________
//
//

ClassImp(TabletStroke);

//==============================================================================

void TabletStroke::_init()
{
  // Override settings from ZGlass
  bUseDispList = true;

  mStartTime = 0;
  bInStroke = false;
}

TabletStroke::TabletStroke(const Text_t* n, const Text_t* t) :
  ZNode(n, t)
{
  _init();
}

TabletStroke::~TabletStroke()
{}

//==============================================================================

Int_t TabletStroke::get_num_points() const
{
  if (mPoints.empty())
  {
    return 0;
  }
  else
  {
    return (Int_t) mPoints.size() - (bInStroke ? 1 : 2);
  }
}

void TabletStroke::get_draw_range(Int_t& min, Int_t& max) const
{
  if (mPoints.empty())
  {
    min = -1; max = -2;
  }
  else
  {
    min = 1;
    max = mPoints.size() - (bInStroke ? 1 : 2);
  }
}

//------------------------------------------------------------------------------

STabletPoint TabletStroke::pre_sym_quadratic(Int_t i0, Int_t i1, Int_t i2) const
{
  STabletPoint d1 = mPoints[i1] - mPoints[i0];
  STabletPoint d2 = mPoints[i2] - mPoints[i0];

  return 2.0f * d1.t / (d2.t - d1.t) * (d1.t * d2 - d2.t * d1) - d1;
}

//==============================================================================

void TabletStroke::BeginStroke()
{
  static const Exc_t _eh("TabletStroke::BeginStroke ");

  if (bInStroke)
    throw _eh + "Already in stroke.";

  bInStroke = true;
  if (bUseDispList)
  {
    bEnableDLAtEnd = true;
    bUseDispList   = false;
  }

  mPoints.clear();
  mPoints.push_back(STabletPoint());
}

void TabletStroke::AddPoint(Float_t x, Float_t y, Float_t t, Float_t p)
{
  static const Exc_t _eh("TabletStroke::AddPoint ");

  if (!bInStroke)
    throw _eh + "Not in stroke.";

  mPoints.push_back(STabletPoint(x, y, 0, t, p));
}

void TabletStroke::EndStroke(Bool_t clip_trailing_zero_pressure_points)
{
  static const Exc_t _eh("TabletStroke::EndStroke ");

  if (!bInStroke)
    throw _eh + "Not in stroke.";

  if (clip_trailing_zero_pressure_points)
  {
    vSTabletPoint_i i = mPoints.end();
    do
    {
      --i;
    }
    while (i != mPoints.begin() && i->p == 0);
    ++i;
    mPoints.erase(i, mPoints.end());
  }

  mPoints.push_back(STabletPoint());

  Int_t NP = mPoints.size() - 2;
  if (NP > 2)
  {
    mPoints.front() = mPoints[1]  + pre_sym_quadratic(1, 2, 3);
    mPoints.back()  = mPoints[NP] + pre_sym_quadratic(NP, NP - 1, NP - 2);
  }
  else if (NP > 1)
  {
    mPoints.front() = 2.0f * mPoints[1] - mPoints[2];
    mPoints.back()  = 2.0f * mPoints[2] - mPoints[1];
  }
  else
  {
    mPoints.front() = mPoints.back() = mPoints[1];
  }

  bInStroke = false;
  if (bEnableDLAtEnd)
  {
    bUseDispList = true;
  }
}

//==============================================================================

#include <Glasses/WSSeed.h>
#include <Glasses/WSPoint.h>

void TabletStroke::MakeWSSeed()
{
  WSSeed *seed = new WSSeed();
  seed->SetFat(true);
  mQueen->CheckIn(seed);
  Add(seed);

  Int_t NP = mPoints.size() - 2;
  for (Int_t i = 1; i <= NP; ++i)
  {
    STabletPoint &prev = mPoints[i-1];
    STabletPoint &curr = mPoints[i];
    STabletPoint &next = mPoints[i+1];
    STabletPoint delta = next - prev;
    // Float_t      d_mag = delta.Mag();

    WSPoint *p = new WSPoint(GForm("Point %d", i));
    p->SetPos(curr.x, curr.y, curr.z);
    p->RotateLF(1, 2, delta.Phi());
    p->SetW(0.02f * curr.p);
    // p->SetS(0.01f * delta.p / d_mag);
    // p->SetT(10.0f * d_mag / delta.t);

    mQueen->CheckIn(p);
    seed->Add(p);
  }
}

//==============================================================================

void TabletStroke::Print()
{
  Int_t np = mPoints.size();
  printf("Foo NP=%d\n", np);
  for (Int_t i = 0; i < np; ++i)
  {
    STabletPoint &p = mPoints[i];
    printf("  %3d  %9.6f %9.6f %9.6f, %9.6f; %6.4f\n",
	   i, p.x, p.y, p.z, p.t, p.p);
  }
}

//==============================================================================

void TabletStroke::MakeHisto(Int_t nbins, Float_t x_edge, Float_t y_edge)
{
  Int_t np = mPoints.size() - 1;
  if (np < 1)
    return;

  Float_t min[2], max[2];

  min[0] = max[0] = mPoints[1].x;
  min[1] = max[1] = mPoints[1].y;

  for (Int_t i = 2; i < np; ++i)
  {
    for (Int_t c = 0; c < 2; ++c)
    {
      min[c] = TMath::Min(min[c], mPoints[i][c]);
      max[c] = TMath::Max(max[c], mPoints[i][c]);
    }
  }
  Float_t xe = x_edge * (max[0] - min[0]);
  Float_t ye = y_edge * (max[1] - min[1]);

  TH2I *h = new TH2I("Points", "Points",
		     nbins, min[0] - xe, max[0] + xe,
		     nbins, min[1] - ye, max[1] + ye);
  for (Int_t i = 1; i < np; ++i)
  {
    h->Fill(mPoints[i].x, mPoints[i].y);
  }

  TCanvas *canvas = XTReqCanvas::Request("Stroke", "Stroke");
  canvas->cd();
  h->Draw();
  XTReqPadUpdate::Update(canvas);
}

void TabletStroke::MakeDeltaHistos(Int_t nbins)
{
  Float_t max_d = 0, max_v = 0, max_t = 0;

  Int_t np = mPoints.size() - 1;
  for (Int_t i = 1; i < np; ++i)
  {
    STabletPoint d = mPoints[i+1] - mPoints[i];
    Float_t dist = d.Mag();
    Float_t vel  = dist / d.t;
    max_d = TMath::Max(max_d, dist);
    max_v = TMath::Max(max_v, vel);
    max_t = TMath::Max(max_t, d.t);
  }
  max_d *= 1.001f;
  max_v *= 1.001f;
  max_t *= 1.001f;
 
  TH1I *hd = new TH1I("Distance", "Distance", nbins, 0, max_d);
  TH1I *hv = new TH1I("Velocity", "Velocity", nbins, 0, max_v);
  TH1I *ht = new TH1I("Delta T",  "Delta T",  nbins, 0, max_t);
  // d .vs. v correlation
  // TH2I *hc = new TH2I(""

  for (Int_t i = 1; i < np; ++i)
  {
    STabletPoint d = mPoints[i+1] - mPoints[i];
    Float_t dist = d.Mag();
    Float_t vel  = dist / d.t;
    hd->Fill(dist);
    hv->Fill(vel);
    ht->Fill(d.t);
  }

  TCanvas *canvas = XTReqCanvas::Request("StrokeDeltas", "Stroke Deltas", 1024, 768, 2, 2);
  canvas->cd(1); hd->Draw();
  canvas->cd(2); hv->Draw();
  canvas->cd(3); ht->Draw();
  XTReqPadUpdate::Update(canvas);
}
 TabletStroke.cxx:1
 TabletStroke.cxx:2
 TabletStroke.cxx:3
 TabletStroke.cxx:4
 TabletStroke.cxx:5
 TabletStroke.cxx:6
 TabletStroke.cxx:7
 TabletStroke.cxx:8
 TabletStroke.cxx:9
 TabletStroke.cxx:10
 TabletStroke.cxx:11
 TabletStroke.cxx:12
 TabletStroke.cxx:13
 TabletStroke.cxx:14
 TabletStroke.cxx:15
 TabletStroke.cxx:16
 TabletStroke.cxx:17
 TabletStroke.cxx:18
 TabletStroke.cxx:19
 TabletStroke.cxx:20
 TabletStroke.cxx:21
 TabletStroke.cxx:22
 TabletStroke.cxx:23
 TabletStroke.cxx:24
 TabletStroke.cxx:25
 TabletStroke.cxx:26
 TabletStroke.cxx:27
 TabletStroke.cxx:28
 TabletStroke.cxx:29
 TabletStroke.cxx:30
 TabletStroke.cxx:31
 TabletStroke.cxx:32
 TabletStroke.cxx:33
 TabletStroke.cxx:34
 TabletStroke.cxx:35
 TabletStroke.cxx:36
 TabletStroke.cxx:37
 TabletStroke.cxx:38
 TabletStroke.cxx:39
 TabletStroke.cxx:40
 TabletStroke.cxx:41
 TabletStroke.cxx:42
 TabletStroke.cxx:43
 TabletStroke.cxx:44
 TabletStroke.cxx:45
 TabletStroke.cxx:46
 TabletStroke.cxx:47
 TabletStroke.cxx:48
 TabletStroke.cxx:49
 TabletStroke.cxx:50
 TabletStroke.cxx:51
 TabletStroke.cxx:52
 TabletStroke.cxx:53
 TabletStroke.cxx:54
 TabletStroke.cxx:55
 TabletStroke.cxx:56
 TabletStroke.cxx:57
 TabletStroke.cxx:58
 TabletStroke.cxx:59
 TabletStroke.cxx:60
 TabletStroke.cxx:61
 TabletStroke.cxx:62
 TabletStroke.cxx:63
 TabletStroke.cxx:64
 TabletStroke.cxx:65
 TabletStroke.cxx:66
 TabletStroke.cxx:67
 TabletStroke.cxx:68
 TabletStroke.cxx:69
 TabletStroke.cxx:70
 TabletStroke.cxx:71
 TabletStroke.cxx:72
 TabletStroke.cxx:73
 TabletStroke.cxx:74
 TabletStroke.cxx:75
 TabletStroke.cxx:76
 TabletStroke.cxx:77
 TabletStroke.cxx:78
 TabletStroke.cxx:79
 TabletStroke.cxx:80
 TabletStroke.cxx:81
 TabletStroke.cxx:82
 TabletStroke.cxx:83
 TabletStroke.cxx:84
 TabletStroke.cxx:85
 TabletStroke.cxx:86
 TabletStroke.cxx:87
 TabletStroke.cxx:88
 TabletStroke.cxx:89
 TabletStroke.cxx:90
 TabletStroke.cxx:91
 TabletStroke.cxx:92
 TabletStroke.cxx:93
 TabletStroke.cxx:94
 TabletStroke.cxx:95
 TabletStroke.cxx:96
 TabletStroke.cxx:97
 TabletStroke.cxx:98
 TabletStroke.cxx:99
 TabletStroke.cxx:100
 TabletStroke.cxx:101
 TabletStroke.cxx:102
 TabletStroke.cxx:103
 TabletStroke.cxx:104
 TabletStroke.cxx:105
 TabletStroke.cxx:106
 TabletStroke.cxx:107
 TabletStroke.cxx:108
 TabletStroke.cxx:109
 TabletStroke.cxx:110
 TabletStroke.cxx:111
 TabletStroke.cxx:112
 TabletStroke.cxx:113
 TabletStroke.cxx:114
 TabletStroke.cxx:115
 TabletStroke.cxx:116
 TabletStroke.cxx:117
 TabletStroke.cxx:118
 TabletStroke.cxx:119
 TabletStroke.cxx:120
 TabletStroke.cxx:121
 TabletStroke.cxx:122
 TabletStroke.cxx:123
 TabletStroke.cxx:124
 TabletStroke.cxx:125
 TabletStroke.cxx:126
 TabletStroke.cxx:127
 TabletStroke.cxx:128
 TabletStroke.cxx:129
 TabletStroke.cxx:130
 TabletStroke.cxx:131
 TabletStroke.cxx:132
 TabletStroke.cxx:133
 TabletStroke.cxx:134
 TabletStroke.cxx:135
 TabletStroke.cxx:136
 TabletStroke.cxx:137
 TabletStroke.cxx:138
 TabletStroke.cxx:139
 TabletStroke.cxx:140
 TabletStroke.cxx:141
 TabletStroke.cxx:142
 TabletStroke.cxx:143
 TabletStroke.cxx:144
 TabletStroke.cxx:145
 TabletStroke.cxx:146
 TabletStroke.cxx:147
 TabletStroke.cxx:148
 TabletStroke.cxx:149
 TabletStroke.cxx:150
 TabletStroke.cxx:151
 TabletStroke.cxx:152
 TabletStroke.cxx:153
 TabletStroke.cxx:154
 TabletStroke.cxx:155
 TabletStroke.cxx:156
 TabletStroke.cxx:157
 TabletStroke.cxx:158
 TabletStroke.cxx:159
 TabletStroke.cxx:160
 TabletStroke.cxx:161
 TabletStroke.cxx:162
 TabletStroke.cxx:163
 TabletStroke.cxx:164
 TabletStroke.cxx:165
 TabletStroke.cxx:166
 TabletStroke.cxx:167
 TabletStroke.cxx:168
 TabletStroke.cxx:169
 TabletStroke.cxx:170
 TabletStroke.cxx:171
 TabletStroke.cxx:172
 TabletStroke.cxx:173
 TabletStroke.cxx:174
 TabletStroke.cxx:175
 TabletStroke.cxx:176
 TabletStroke.cxx:177
 TabletStroke.cxx:178
 TabletStroke.cxx:179
 TabletStroke.cxx:180
 TabletStroke.cxx:181
 TabletStroke.cxx:182
 TabletStroke.cxx:183
 TabletStroke.cxx:184
 TabletStroke.cxx:185
 TabletStroke.cxx:186
 TabletStroke.cxx:187
 TabletStroke.cxx:188
 TabletStroke.cxx:189
 TabletStroke.cxx:190
 TabletStroke.cxx:191
 TabletStroke.cxx:192
 TabletStroke.cxx:193
 TabletStroke.cxx:194
 TabletStroke.cxx:195
 TabletStroke.cxx:196
 TabletStroke.cxx:197
 TabletStroke.cxx:198
 TabletStroke.cxx:199
 TabletStroke.cxx:200
 TabletStroke.cxx:201
 TabletStroke.cxx:202
 TabletStroke.cxx:203
 TabletStroke.cxx:204
 TabletStroke.cxx:205
 TabletStroke.cxx:206
 TabletStroke.cxx:207
 TabletStroke.cxx:208
 TabletStroke.cxx:209
 TabletStroke.cxx:210
 TabletStroke.cxx:211
 TabletStroke.cxx:212
 TabletStroke.cxx:213
 TabletStroke.cxx:214
 TabletStroke.cxx:215
 TabletStroke.cxx:216
 TabletStroke.cxx:217
 TabletStroke.cxx:218
 TabletStroke.cxx:219
 TabletStroke.cxx:220
 TabletStroke.cxx:221
 TabletStroke.cxx:222
 TabletStroke.cxx:223
 TabletStroke.cxx:224
 TabletStroke.cxx:225
 TabletStroke.cxx:226
 TabletStroke.cxx:227
 TabletStroke.cxx:228
 TabletStroke.cxx:229
 TabletStroke.cxx:230
 TabletStroke.cxx:231
 TabletStroke.cxx:232
 TabletStroke.cxx:233
 TabletStroke.cxx:234
 TabletStroke.cxx:235
 TabletStroke.cxx:236
 TabletStroke.cxx:237
 TabletStroke.cxx:238
 TabletStroke.cxx:239
 TabletStroke.cxx:240
 TabletStroke.cxx:241
 TabletStroke.cxx:242
 TabletStroke.cxx:243
 TabletStroke.cxx:244
 TabletStroke.cxx:245
 TabletStroke.cxx:246
 TabletStroke.cxx:247
 TabletStroke.cxx:248
 TabletStroke.cxx:249
 TabletStroke.cxx:250
 TabletStroke.cxx:251
 TabletStroke.cxx:252
 TabletStroke.cxx:253
 TabletStroke.cxx:254
 TabletStroke.cxx:255
 TabletStroke.cxx:256
 TabletStroke.cxx:257
 TabletStroke.cxx:258
 TabletStroke.cxx:259
 TabletStroke.cxx:260
 TabletStroke.cxx:261
 TabletStroke.cxx:262
 TabletStroke.cxx:263
 TabletStroke.cxx:264
 TabletStroke.cxx:265
 TabletStroke.cxx:266
 TabletStroke.cxx:267
 TabletStroke.cxx:268
 TabletStroke.cxx:269
 TabletStroke.cxx:270
 TabletStroke.cxx:271
 TabletStroke.cxx:272
 TabletStroke.cxx:273
 TabletStroke.cxx:274
 TabletStroke.cxx:275
 TabletStroke.cxx:276
 TabletStroke.cxx:277
 TabletStroke.cxx:278