summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/sift/src/matlab/sift_gendoc.pl
blob: 6bc0b5e1f385d48df55d13c5bf31c9b36ca78d35 (plain) (blame)
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
#!/usr/bin/perl -w 
## file:        sift_gendoc.pl
## author:      Andrea Vedaldi
## description: Summarize MATLAB M-Files docs

# AUTORIGHTS
# Copyright (c) 2006 The Regents of the University of California.
# All Rights Reserved.
# 
# Created by Andrea Vedaldi
# UCLA Vision Lab - Department of Computer Science
# 
# Permission to use, copy, modify, and distribute this software and its
# documentation for educational, research and non-profit purposes,
# without fee, and without a written agreement is hereby granted,
# provided that the above copyright notice, this paragraph and the
# following three paragraphs appear in all copies.
# 
# This software program and documentation are copyrighted by The Regents
# of the University of California. The software program and
# documentation are supplied "as is", without any accompanying services
# from The Regents. The Regents does not warrant that the operation of
# the program will be uninterrupted or error-free. The end-user
# understands that the program was developed for research purposes and
# is advised not to rely exclusively on the program for any reason.
# 
# This software embodies a method for which the following patent has
# been issued: "Method and apparatus for identifying scale invariant
# features in an image and use of same for locating an object in an
# image," David G. Lowe, US Patent 6,711,293 (March 23,
# 2004). Provisional application filed March 8, 1999. Asignee: The
# University of British Columbia.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
# INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND
# ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF
# CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
# BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
# MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

# Debugging level
$verb = 0 ;

# PDF document location
$pdfdoc = 'sift.pdf' ;

# This is the queue of directories to process.
# It gets filled recursively.
@dir_fifo = ('.') ;

# This will hold an entry for each m-file found
%mfiles = () ;

# This will hold an entry for each module found 
%modules = () ;

# #ARGV is the index of the last element, which is 0
if ($#ARGV == 0) {
    open(FOUT,">$ARGV[0]") ;
    print STDERR "Writing to file '$ARGV[0]'.\n" if $verb ;
} else {
    *FOUT= *STDOUT; 
    print STDERR "Using standard output.\n" if $verb ; 
}

# Each module is a subdirectory. The subdirectory is used
# as the module ID.

while ($module_path = shift(@dir_fifo)) { 
    print STDERR "=> '$module_path'\n" if $verb ;
    
    # get a first version of the module name
    $module_path =~ m/.*\/([^\\]+)$/ ;
    $module_name = $1 ;

    # start a new module    
    $module = {
        'id'          => $module_path,
        'path'        => $module_path,
        'name'        => $module_name,
        'mfiles'      => [],
        'description' => ""
    } ;
    
    # .................................................................
    opendir(DIRHANDLE, $module->{'path'}) ;
    FILE: foreach (sort readdir(DIRHANDLE)) {
        $name = $_ ;
        $path = $module->{'path'} . "/" . $_ ;

        # push if a directory
        $_ = $path ;
        if (-d) {
            next FILE if /(\.$|\.\.$)/ ;
            push( @dir_fifo, "$_" ) ;
            next FILE ;
        }
        
        # parse if .m and not test_
        next FILE unless (/.+\.m$/) ;
        next FILE if (/test_.x*/) ;
        $name =~ m/(.*).m/ ;
        $name = $1 ;
        print STDERR "  . m-file '$name'\n" if $verb ;
        my ($brief,$description) = get_comment($path) ;

        # topic description?
        if (/overview/) {
            print STDERR "    * module description\n" if $verb ;
            $module->{'id'}          = $name ;
            $module->{'name'}        = $brief ;
            $module->{'description'} = $description ;
            next FILE ;
        }

        # use names as IDs
        $id = $name ;
        $id =~ tr/A-Z/a-z/ ;

        # create a new mfile object
        $mfile = {
            'id'          => $id,
            'path'        => $path,
            'name'        => $name,
            'brief'       => $brief,
            'module'      => $module,
            'description' => $description
        } ;
        
        # add a reference to this mfile
        # object to the global mfile list
        $mfiles{$id} = $mfile ;

        # add a reference to this mfile
        # object to the current module
        push( @{$module->{'mfiles'}}, $mfile) ;
    }
    closedir(DIRHANDLE) ;
    # ................................................................

    # add a reference to the current module to the global
    # module list
    $modules{$module->{'id'}} = $module ;
}

# ....................................................................
#                                                  write documentation
# ....................................................................

print FOUT <<EOF;
<html>
  <head>
    <link href="default.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
EOF

# sort modules by path
sub criterion { $modules{$a}->{'path'} cmp $modules{$b}->{'path'} ; }

MODULE:
foreach $id ( sort criterion keys %modules ) {
    my $module = $modules{$id} ;   
    my $rich_description = make_rich($module->{'description'}) ;

    next MODULE if $#{$module->{'mfiles'}} < 0 and length($rich_description) == 0;

    print FOUT <<EOF;
    <div class='module' id='$module->{"id"}'>
      <h1>$module->{'name'}</h1>
      <div class='index'>
        <h1>Module contents</h1>
        <ul>
EOF
    foreach( @{$module->{'mfiles'}} ) {
        print FOUT "        <li><a href='#$_->{'id'}'>" 
                 . "$_->{'name'}</a></li>\n" ;
    }
    print FOUT <<EOF;
        </ul>
      </div>
      <pre>
      $rich_description
      </pre>
      <div class="footer">
      </div>
    </div>
EOF
}

foreach $id (sort keys %mfiles) {
    my $mfile = $mfiles{$id} ;
    my $rich_description = make_rich($mfile->{'description'}) ;

    print FOUT <<EOF;
    <div class="mfile" id='$mfile->{"id"}'>
      <h1>
       <span class="name">$mfile->{"name"}</span>
       <span class="brief">$mfile->{"brief"}</span>
      </h1>
      <pre>
$rich_description
      </pre>
      <div class="footer">
        <a href="#$mfile->{'module'}->{'id'}">
        $mfile->{'module'}->{'name'}
        </a>
      </div>
    </div>
EOF
}

print FOUT "</body></html>" ;

# Close file
close FOUT ;


# -------------------------------------------------------------------------
sub get_comment {
# -------------------------------------------------------------------------
    local $_ ;
    my $full_name = $_[0] ;
    my @comment = () ;
    
    open IN,$full_name ;
  SCAN: 
    while( <IN> ) {
        next if /^function/ ;
        last SCAN unless ( /^%/ ) ;
        push(@comment, substr("$_",1)) ;
    }
    close IN ;

    my $brief = "" ;
    if( $#comment >= 0 && $comment[0] =~ m/^\s*\w+\s+(.*)$/ ) {
        $brief = $1 ;
        splice (@comment, 0, 1) ;
    }

    # from the first line
    return ($brief, join("",@comment)) ;
}

# -------------------------------------------------------------------------
sub make_rich {
# -------------------------------------------------------------------------
    local $_ = $_[0] ;
    s/([A-Z]+[A-Z0-9_]*)\(([^\)]*)\)/${\make_link($1,$2)}/g ;
    s/PDF:([A-Z0-9_\-:.]+[A-Z0-9])/${\make_pdf_link($1)}/g ;
    return $_ ;
}

# -------------------------------------------------------------------------
sub make_link {
# -------------------------------------------------------------------------
    local $_ ;
    my $name = $_[0] ;
    my $arg  = $_[1] ;
    my $id   = $name ;

    # convert name to lower case and put into $_
    $id =~ tr/A-Z/a-z/ ;
    
    # get mfile
    my $mfile = $mfiles{$id} ;
    my $module = $modules{$id} ;
    
    # return as appropriate    
    if($mfile) {
        return "<a href='#$id'>" . $name . "</a>" . "(" . $arg . ")" ;
    } elsif($module) {
      return "<a class='module' href='#$id'>" . $name . 
          "</a>" . "(" . $arg . ")" ;
    } else {
        return $name . "(" . $arg .")" ;
    }
}


# -------------------------------------------------------------------------
sub make_pdf_link {
# -------------------------------------------------------------------------
    local $_ ;
    my $name = $_[0] ;
    my $id   = $name ;

    # convert name to lower case and put into $_
    $id =~ tr/A-Z/a-z/ ;
    
    return "<a href='${pdfdoc}#$id'>PDF:$1</a>" ;
}