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
|
//
// Zinnia: Online hand recognition system with machine learning
//
// $Id: character.cpp 17 2009-04-05 11:40:32Z taku-ku $;
//
// Copyright(C) 2008 Taku Kudo <taku@chasen.org>
//
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <cctype>
#include "common.h"
#include "zinnia.h"
#include "sexp.h"
#include "scoped_ptr.h"
namespace zinnia {
class CharacterImpl: public Character {
public:
void set_value(const char *str) {
value_.assign(str, std::strlen(str));
}
void set_value(const char *str, size_t length) {
value_.assign(str, length);
}
const char *value() const {
return value_.c_str();
}
void set_width(size_t width) {
width_ = width;
}
void set_height(size_t height) {
height_ = height;
}
size_t width() const { return width_; }
size_t height() const { return height_; }
void clear() { strokes_.clear(); }
bool add(size_t id, int x, int y);
size_t strokes_size() const {
return strokes_.size();
}
size_t stroke_size(size_t id) const {
return (strokes_.size() <= id) ?
static_cast<size_t>(-1) : strokes_[id].size();
}
int x(size_t id, size_t i) const {
return (id >= strokes_.size() || i >= strokes_[id].size()) ?
-1 : strokes_[id][i].x;
}
int y(size_t id, size_t i) const {
return (id >= strokes_.size() || i >= strokes_[id].size()) ?
-1 : strokes_[id][i].y;
}
bool parse(const char *str, size_t length);
bool parse(const char *str) {
return parse(str, std::strlen(str));
}
bool toString(char *buf, size_t length) const;
const char *what() { return what_.str(); }
CharacterImpl(): width_(300), height_(300) {}
virtual ~CharacterImpl() { clear(); }
private:
struct Dot {
int x;
int y;
};
std::vector<std::vector<Dot> > strokes_;
std::string value_;
size_t width_;
size_t height_;
whatlog what_;
scoped_ptr<Sexp> sexp_;
};
bool CharacterImpl::add(size_t id, int x, int y) {
strokes_.resize(id + 1);
Dot d;
d.x = x;
d.y = y;
strokes_[id].push_back(d);
return true;
}
// parse S-expression
// (character
// (value A)
// (width 100)
// (height 100)
// (strokes ( (0 1) (1 2) ) ( (0 1) (1 2) ) ) )
bool CharacterImpl::parse(const char *str, size_t length) {
clear();
if (!sexp_.get()) {
sexp_.reset(new Sexp);
}
sexp_->free();
char *begin = const_cast<char *>(str);
const char *end = str + length;
const Sexp::Cell *root_cell = sexp_->read(&begin, end);
if (!root_cell) {
sexp_->free();
return false;
}
const Sexp::Cell *ccel = root_cell->car();
if (!ccel->is_atom() || std::strcmp("character", ccel->atom()) != 0) {
sexp_->free();
return false;
}
for (const Sexp::Cell *it = root_cell->cdr(); it; it = it->cdr()) {
const Sexp::Cell *cell= it->car();
if (cell->car() && cell->car()->is_atom() &&
cell->cdr() && cell->cdr()->car() &&
cell->cdr()->car()->is_atom()) {
const char *name = cell->car()->atom();
const char *value = cell->cdr()->car()->atom();
if (std::strcmp("value", name) == 0) {
set_value(value);
} else if (std::strcmp("width", name) == 0) {
set_width(std::atoi(value));
} else if (std::strcmp("height", name) == 0) {
set_height(std::atoi(value));
}
}
if (cell->car() && cell->car()->is_atom() &&
cell->cdr() && cell->cdr()->car() &&
cell->cdr()->car()->is_cons() &&
std::strcmp("strokes", cell->car()->atom()) == 0) {
int id = 0;
for (const Sexp::Cell *st = cell->cdr(); st; st = st->cdr()) {
for (const Sexp::Cell *dot = st->car(); dot; dot = dot->cdr()) {
if (dot->car() && dot->car()->car() &&
dot->car()->car()->is_atom() &&
dot->car()->cdr() &&
dot->car()->cdr()->car() &&
dot->car()->cdr()->car()->is_atom()) {
const int x = std::atoi(dot->car()->car()->atom());
const int y = std::atoi(dot->car()->cdr()->car()->atom());
add(id, x, y);
}
}
++id;
}
}
}
return true;
}
bool CharacterImpl::toString(char *str, size_t length) const {
std::ostringstream os;
os << "(character (value " << value() << ")";
os << "(width " << width() << ")";
os << "(height " << height() << ")";
os << "(stroeks ";
for (size_t id = 0; id < strokes_size(); ++id) {
os << "(";
for (size_t s = 0; s < stroke_size(id); ++s) {
os << "(" << x(id, s) << " " << y(id, s) << ")";
}
os << ")";
}
os << ")";
const std::string &buf = os.str();
if (buf.size() < length) {
memcpy(str, buf.data(), buf.size());
str[buf.size()] = '\0';
}
return true;
}
Character *Character::create() {
return new CharacterImpl;
}
Character *createCharacter() {
return new CharacterImpl;
}
}
|